Abhinand2001's picture
Update app.py
9578310
raw
history blame
2.79 kB
import streamlit as st
import numpy as np
import pickle
import pandas as pd
from ultralytics import YOLO
import os
import glob
import cv2
from statistics import mode
letter_map={0: 'അ', 1: 'ആ', 2: 'ച', 3: 'ഇ', 4: 'ജ', 5: 'ക', 6: 'ല', 7: 'ൾ', 8: 'ണ', 9: 'ന', 10: 'പ', 11: 'ര', 12: 'ർ', 13: 'റ', 14: 'സ', 15: 'ട', 16: 'ത', 17: 'വ', 18: 'യ', 19: 'ഴ'}
st.header('Sign Detection')
model = YOLO("best.onnx",task='detect')
def extract_frames(uploaded_file, output_folder):
input_name = os.path.splitext(uploaded_file.name)[0]
if not os.path.exists(output_folder):
os.makedirs(output_folder)
content = uploaded_file.getvalue()
temp_file_path = os.path.join(output_folder, "temp_video.mp4")
with open(temp_file_path, "wb") as temp_file:
temp_file.write(content)
video_capture = cv2.VideoCapture(temp_file_path)
frame_count = 0
while True:
ret, frame = video_capture.read()
if not ret:
break
frame_filename = os.path.join(output_folder, f"{input_name}-{frame_count:04d}.jpg")
cv2.imwrite(frame_filename, frame)
frame_count += 1
video_capture.release()
st.write(f"Extracted {frame_count} frames from uploaded video to {output_folder}")
os.remove(temp_file_path)
uploaded_file = st.file_uploader("Choose a video file", type=["mp4"])
if uploaded_file is not None:
st.video(uploaded_file)
st.text("Uploaded Video")
if st.button("Submit"):
output_folder ="output"
files = glob.glob(os.path.join(output_folder, '*'))
for file in files:
os.remove(file)
extract_frames(uploaded_file, output_folder)
preds=[]
t=[]
cl = []
previous_element = None
word=''
for file in sorted(os.listdir(output_folder)):
frame=os.path.join(output_folder,file)
print(frame)
res=model.predict(frame)
try:
preds.append(int(res[0].boxes.data[0][-1]))
except:
preds.append(-1)
st.write(preds)
if len(preds)%25:
a=0
b=a+25
for i in range((len(preds)//25)+1):
t.append(mode(preds[a:b]))
a,b=b,b+25
elif len(preds)%25:
a=0
b=a+25
for i in range(len(preds)//25):
t.append(mode(preds[a:b]))
a,b=b,b+25
for element in t:
if element != -1:
if element != previous_element:
cl.append(element)
previous_element = element
for i in cl:
word+=letter_map[i]
st.write(word)