denizaybey commited on
Commit
870fca9
·
verified ·
1 Parent(s): 73cae6d

Upload 4 files

Browse files
Files changed (4) hide show
  1. README.md +1 -1
  2. app.py +61 -0
  3. model.pt +3 -0
  4. requirements.txt +5 -0
README.md CHANGED
@@ -9,4 +9,4 @@ app_file: app.py
9
  pinned: false
10
  ---
11
 
12
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
9
  pinned: false
10
  ---
11
 
12
+ Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
app.py ADDED
@@ -0,0 +1,61 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import io
2
+ import numpy
3
+ import gradio
4
+ import spaces
5
+ import moviepy
6
+ import supervision
7
+ from PIL import Image
8
+ from ultralytics import YOLOE
9
+
10
+
11
+ @spaces.GPU
12
+ def inference(video_path):
13
+ model = YOLOE("./model.pt")
14
+ names = ["person", "vehicle"]
15
+ model.set_classes(names, model.get_text_pe(names))
16
+ clip = moviepy.VideoFileClip(video_path)
17
+ results = []
18
+ for i, frame in enumerate(clip.iter_frames(fps=1)):
19
+ image = Image.fromarray(numpy.uint8(frame))
20
+ result = model.predict(frame, imgsz=640, conf=0.25, iou=0.7)
21
+ detections = supervision.Detections.from_ultralytics(result[0])
22
+ resolution_wh = image.size
23
+ thickness = supervision.calculate_optimal_line_thickness(resolution_wh=resolution_wh)
24
+ text_scale = supervision.calculate_optimal_text_scale(resolution_wh=resolution_wh)
25
+ labels = [
26
+ f"{class_name} {confidence:.2f}"
27
+ for class_name, confidence
28
+ in zip(detections['class_name'], detections.confidence)
29
+ ]
30
+ annotated_image = image.copy()
31
+ annotated_image = supervision.MaskAnnotator(color_lookup=supervision.ColorLookup.INDEX, opacity=0.4).annotate(
32
+ scene=annotated_image, detections=detections)
33
+ annotated_image = supervision.BoxAnnotator(color_lookup=supervision.ColorLookup.INDEX,
34
+ thickness=thickness).annotate(
35
+ scene=annotated_image, detections=detections)
36
+ annotated_image = supervision.LabelAnnotator(color_lookup=supervision.ColorLookup.INDEX, text_scale=text_scale,
37
+ smart_position=True).annotate(
38
+ scene=annotated_image, detections=detections, labels=labels)
39
+ results.append(annotated_image)
40
+ frames = [numpy.array(img) for img in results]
41
+ output_clip = moviepy.ImageSequenceClip(frames, fps=1)
42
+ buf = io.BytesIO()
43
+ output_clip.write_videofile(buf, codec="libx264", audio=False)
44
+ clip.close()
45
+ buf.seek(0)
46
+ return buf
47
+
48
+
49
+ def gradio_interface(video_file):
50
+ output_video = inference(video_file.name)
51
+ return output_video
52
+
53
+
54
+ if __name__ == "__main__":
55
+ gradio.Interface(
56
+ fn=gradio_interface,
57
+ inputs=gradio.Video(type="file"),
58
+ outputs=gradio.Video(),
59
+ title="Video Object Detection",
60
+ description="Upload a video to run object detection using YOLOE.",
61
+ ).launch()
model.pt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:bf39e17c356f4b73977aa978bbdf6d19d0830e6bff46727d2d1f260414e41e0f
3
+ size 74264838
requirements.txt ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ ultralytics
2
+ supervision
3
+ moviepy
4
+ spaces
5
+ numpy