|
import cv2 |
|
import csv |
|
import tempfile |
|
import gradio as gr |
|
from ultralytics import YOLO |
|
|
|
def process_video(video_file): |
|
|
|
colors = [ |
|
(255, 0, 0), |
|
(50, 205, 50), |
|
(0, 0, 255), |
|
(255, 255, 0), |
|
(255, 0, 255), |
|
(255, 140, 0), |
|
(128, 0, 128), |
|
(0, 128, 128) |
|
] |
|
|
|
|
|
class_names = ['Hymenoptera', 'Mantodea', 'Odonata', 'Orthoptera', 'Coleoptera', 'Lepidoptera', 'Hemiptera'] |
|
|
|
|
|
model = YOLO("insect_detection4.pt") |
|
|
|
|
|
cap = cv2.VideoCapture(video_file) |
|
|
|
|
|
csv_file = tempfile.NamedTemporaryFile(mode='w', delete=False) |
|
writer = csv.writer(csv_file) |
|
writer.writerow(["frame", "id", "class", "x", "y", "w", "h"]) |
|
|
|
frame_id = 0 |
|
|
|
|
|
annotated_frames = [] |
|
|
|
|
|
while cap.isOpened(): |
|
|
|
success, frame = cap.read() |
|
|
|
if success: |
|
frame_id += 1 |
|
|
|
|
|
results = model.track(frame, persist=True) |
|
|
|
for result in results: |
|
boxes = result.boxes.cpu().numpy() |
|
confidences = boxes.conf |
|
class_ids = boxes.cls |
|
|
|
for i, box in enumerate(boxes): |
|
class_id = int(class_ids[i]) |
|
confidence = confidences[i] |
|
|
|
color = colors[class_id % len(colors)] |
|
label = f'{class_names[class_id]}: {confidence:.2f}' |
|
|
|
|
|
cv2.rectangle(frame, (int(box.xyxy[0][0]), int(box.xyxy[0][1])), |
|
(int(box.xyxy[0][2]), int(box.xyxy[0][3])), color, 2) |
|
|
|
|
|
label_size, _ = cv2.getTextSize(label, cv2.FONT_HERSHEY_SIMPLEX, 2.0, 2) |
|
label_y = max(int(box.xyxy[0][1]) - label_size[1], 0) |
|
cv2.rectangle(frame, (int(box.xyxy[0][0]), label_y - label_size[1]), |
|
(int(box.xyxy[0][0]) + label_size[0], label_y + label_size[1]), color, -1) |
|
cv2.putText(frame, label, (int(box.xyxy[0][0]), label_y), cv2.FONT_HERSHEY_SIMPLEX, 2.0, (255, 255, 255), 2) |
|
|
|
|
|
writer.writerow([frame_id, box.id, int(box.cls[0]), box.xywh[0][0], box.xywh[0][1], |
|
box.xywh[0][2], box.xywh[0][3]]) |
|
|
|
|
|
annotated_frames.append(frame) |
|
|
|
else: |
|
break |
|
|
|
|
|
cap.release() |
|
|
|
|
|
output_video_path = tempfile.NamedTemporaryFile(suffix='.mp4').name |
|
height, width, _ = annotated_frames[0].shape |
|
fourcc = cv2.VideoWriter_fourcc(*'mp4v') |
|
out = cv2.VideoWriter(output_video_path, fourcc, 30.0, (width, height)) |
|
for frame in annotated_frames: |
|
out.write(frame) |
|
out.release() |
|
|
|
|
|
csv_file.close() |
|
|
|
return output_video_path, csv_file.name |
|
|
|
|
|
inputs = gr.Video(label="Input Video") |
|
outputs = [gr.Video(label="Annotated Video"), gr.File(label="CSV File")] |
|
|
|
gradio_app=gr.Interface(fn=process_video, inputs=inputs, outputs=outputs) |
|
|
|
|
|
|
|
if __name__ == "__main__": |
|
gradio_app.launch() |