File size: 2,786 Bytes
eeb4a26
 
 
 
 
 
 
 
 
 
 
 
 
 
9578310
eeb4a26
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9578310
eeb4a26
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
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)