CoolFace
Apppublic

KAHRAMAN42/object_detection_for_cattle

sourceHugging Faceapache-2.0updated 3y agoView on Hugging Face
0likes
app.py54 linesDownload Raw Back to root
1import cv22from ultralytics import YOLO3import numpy as np4import os5import gradio as gr6 7 8pt= "best.pt"9example_video = "cows-and-cows-and-cows (online-video-cutter.com).mp4"10output_video = "output_video.mp4"11 12def fonk(video_path):13  14  model=YOLO(pt)15  cap=cv2.VideoCapture(video_path)  16 17  frame_width = int(cap.get(3)) 18  frame_height = int(cap.get(4))19  size = (frame_width, frame_height)20  output_video= "output_video.mp4"21  writer = cv2.VideoWriter(output_video,  22                         cv2.VideoWriter_fourcc(*"DIVX"), 23                         10, size) 24 25  26  while True:27    ret, frame= cap.read()28 29    if ret!=True:30      break31 32    results= model(frame)33    for result in results:34        if result.boxes is not None and len(result.boxes):35            box = result.boxes36            x1, y1, x2, y2 = map(int, box.xyxy[0])37            print(x1, y1, x2, y2)38            frame = cv2.rectangle(frame, (x1, y1), (x2, y2), (0, 255, 0), 2)39            writer.write(frame)40      41      42  writer.release()43  cap.release()44  return output_video45 46demo = gr.Interface(fonk,47                    inputs= gr.Video(),48                    outputs=gr.Video(),49                    examples=[example_video],50                    title= "cows",51                    cache_examples=True)52demo.launch()53    54