AayushGenerativeAI's picture
Create app.py
c37d2fa verified
Raw History Blame Contribute Delete
4.29 kB
import gradio as gr
import cv2
import random
import requests
from ultralytics import YOLO
import numpy as np
my_file = open('coco2.txt', 'r')
data = my_file.read()
class_list = data.split('\n')
my_file.close()
# Loading YOLO model
model = YOLO('best.pt')
#Path for video in example
video_path = [['video.mp4']]
# Function to get frames from the video and preprocess them
def show_preds_video(video_path):
cap = cv2.VideoCapture(video_path)# capture the video on video path which is the attribute of f show_preds_video
while(cap.isOpened()): # This starts a while loop that continues as long as the video capture object is open.
ret, frame = cap.read() # Capture frame-by-frame
if ret:
frame_copy = frame.copy() # copy the frame so not make changes in origina frame
outputs = model.predict(source=frame_copy)
# print(outputs.cpu().numpy()) # run inference on the frame. The 'predict' method of the model returns detection results in tensor form
results = outputs[0].cpu().numpy() # The detection results are extracted from the 'outputs' tensor, converted to a NumPy array, and stored in the 'results' variable
print(results)
for i, det in enumerate(results.boxes.xyxy): # This loop iterates over each detected object in the frame, 'results.boxes.xyxy' contains the bounding box coordinates (x1, y1, x2, y2) of each detected object.
# This function draws a rectangle around each detected object on the
frame_copy = cv2.rectangle(
frame_copy,
(int(det[0]), int(det[1])),
(int(det[2]), int(det[3])),
color=(0, 255, 0),
thickness=2,
lineType=cv2.LINE_AA
)
boxes = results[i].boxes
box = boxes[0] # Assuming you only want to consider the first box in each detection
clsID = box.cls
conf = box.conf
bb = box.xyxy
# Access individual bounding box coordinates and convert them to Python scalars
x1, y1, x2, y2 = bb[0].tolist()
font = cv2.FONT_HERSHEY_SIMPLEX
frame_copy = cv2.putText(
frame_copy,
class_list[int(clsID)] + " " + str(round(conf.item() * 100, 3)) + "%",
(int(x1), int(y1) - 10),
font,
1,
(0, 0, 255),
2,
)
# for j in range(len(outputs)):
# boxes = outputs[j].boxes
# box = boxes[0] # Assuming you only want to consider the first box in each detection
# clsID = box.cls
# conf = box.conf
# bb = box.xyxy
# # Access individual bounding box coordinates and convert them to Python scalars
# x1, y1, x2, y2 = bb[0].tolist()
# font = cv2.FONT_HERSHEY_SIMPLEX
# frame_copy = cv2.putText(
# frame_copy,
# class_list[int(clsID)] + " " + str(round(conf.item() * 100, 3)) + "%",
# (int(x1), int(y1) - 10),
# font,
# 1,
# (0, 0, 255),
# 2,
# )
yield cv2.cvtColor(frame_copy, cv2.COLOR_BGR2RGB) # Yields the processed frame in RGB format. This allows the processed frames to be displayed or further processed outside of this function.
inputs_video = [
gr.components.Video(label="Input Video"),
]
outputs_video = [
gr.components.Image(label="Output Image"),
]
interface_video = gr.Interface(
fn=show_preds_video,
inputs=inputs_video,
outputs=outputs_video,
title="PPE detector",
examples=video_path,
cache_examples=False,
)
gr.TabbedInterface(
[interface_video],['video_inference']
).queue().launch(share=True)