File size: 1,642 Bytes
8768717
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
from gradio_client import Client , handle_file
import cv2
import os
from PIL import Image

clientImgPipeLn = Client("dj-dawgs-ipd/IPD_IMAGE_PIPELINE")

def predict(video_path):
    cap = cv2.VideoCapture(video_path)
    fps = int(cap.get(cv2.CAP_PROP_FPS))  
    frame_interval = fps * 2

    frame_count = 0
    success = True

    temp_dir = "temp_frames"
    os.makedirs(temp_dir, exist_ok=True)
    
    res = 'not_hate'

    while success:
        success, frame = cap.read()
        if frame_count % frame_interval == 0 and success:
            temp_image_path = os.path.join(temp_dir, f"frame_{frame_count // fps}s.jpg")
            cv2.imwrite(temp_image_path, frame)

            response = clientImgPipeLn.predict(
                image=handle_file(temp_image_path),
                api_name="/predict"
            )
            print(f"Response for frame at {frame_count // fps}s: {response}")
            if(response[0]['label'] == 'hate'):
                res = 'hate'
                break

        frame_count += 1

    cap.release()

    for file in os.listdir(temp_dir):
        os.remove(os.path.join(temp_dir, file))
    os.rmdir(temp_dir)

    print("prediction successful")
    
    return res
    
iface = gr.Interface(fn=predict,
                     inputs = gr.Video(),
                     outputs=[gr.Label(label = "Class")],
                     title = "Hate Speech Detection in Video",
                     description = "Detect hateful symbols or text in Video"
)
                     
if __name__ == "__main__":
    iface.launch()