siddhantuniyal's picture
Upload 2 files
8768717 verified
raw
history blame
1.64 kB
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()