crimson78 commited on
Commit
a799a8a
·
verified ·
1 Parent(s): 89fe99b

Delete best_int8_openvino_model/best_int8_openvino_model/app.py

Browse files
best_int8_openvino_model/best_int8_openvino_model/app.py DELETED
@@ -1,109 +0,0 @@
1
- from ultralytics import YOLO
2
- import cv2
3
- import gradio as gr
4
- from PIL import Image
5
- from huggingface_hub import snapshot_download
6
- import os
7
- import numpy as np
8
-
9
- # Model path and loading function
10
- model_path = "/Users/LeeTM/Downloads/best_int8_openvino_model"
11
-
12
- def load_model(repo_id):
13
- print("Loading model, please wait...")
14
- download_dir = snapshot_download(repo_id)
15
- path = os.path.join(download_dir, "best_int8_openvino_model")
16
- detection_model = YOLO(path, task='detect')
17
- return detection_model
18
-
19
- def process_image(pilimg):
20
- # Default values for confidence and IOU
21
- conf = 0.7 # Confidence threshold
22
- iou = 0.7 # IOU threshold
23
-
24
- # Apply the YOLOv8 model for object detection
25
- result = detection_model.predict(pilimg, conf=conf, iou=iou)
26
-
27
- # Plot the results (bounding boxes) on the image
28
- img_bgr = result[0].plot() # This will draw the bounding boxes
29
- out_pilimg = Image.fromarray(img_bgr[..., ::-1]) # Convert to RGB-order PIL image
30
-
31
- # Collect labels and confidences for the detected objects
32
- labels = [detection_model.names[int(det.cls)] for det in result[0].boxes]
33
- confidences = [f"{float(det.conf):.2f}" for det in result[0].boxes]
34
-
35
- detection_details = "\n".join([f"{label}: {conf}" for label, conf in zip(labels, confidences)])
36
-
37
- return out_pilimg, detection_details
38
-
39
- def process_video(video_path):
40
- # Default values for confidence and IOU
41
- conf = 0.7 # Confidence threshold
42
- iou = 0.7 # IOU threshold
43
-
44
- cap = cv2.VideoCapture(video_path)
45
-
46
- # Get video details
47
- frame_width = int(cap.get(3))
48
- frame_height = int(cap.get(4))
49
- fps = int(cap.get(5))
50
-
51
- # Output video settings
52
- out_path = "output_video.mp4"
53
- out = cv2.VideoWriter(out_path, cv2.VideoWriter_fourcc(*'mp4v'), fps, (frame_width, frame_height))
54
-
55
- while cap.isOpened():
56
- ret, frame = cap.read()
57
- if not ret:
58
- break
59
-
60
- # Convert the frame to a PIL image (YOLO uses RGB format)
61
- pil_img = Image.fromarray(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB))
62
-
63
- # Apply the YOLOv8 model for object detection
64
- result = detection_model.predict(pil_img, conf=conf, iou=iou)
65
-
66
- # Plot the results (bounding boxes) on the frame
67
- img_bgr = result[0].plot() # This will draw the bounding boxes
68
- frame_bgr = cv2.cvtColor(np.array(img_bgr), cv2.COLOR_RGB2BGR)
69
-
70
- # Write the processed frame to the output video
71
- out.write(frame_bgr)
72
-
73
- cap.release()
74
- out.release()
75
-
76
- return None, out_path # Return None for image, and video path for the video output
77
-
78
- # Model Repository
79
- REPO_ID = "crimson78/pokemon_ash_pikachu_yolov8"
80
- detection_model = load_model(REPO_ID)
81
-
82
- # Gradio Interface
83
- def detect_objects(input_file):
84
- # Check if the input is a video or an image
85
- if input_file.endswith(('.mp4', '.mov', '.avi', '.mkv')):
86
- # Process as video and return None for image
87
- return process_video(input_file)
88
- else:
89
- # Process as image and return video as None
90
- pil_img = Image.open(input_file)
91
- return process_image(pil_img)
92
-
93
- # Gradio Interface
94
- gui = gr.Interface(
95
- fn=detect_objects,
96
- inputs=[
97
- gr.File(label="Upload an Image or Video", type="filepath"), # File input (both image and video supported)
98
- ],
99
- outputs=[
100
- gr.Image(type="pil", label="Detection Results"), # Image output for images
101
- gr.Video(label="Detection Results") # Video output for videos
102
- ],
103
- title="YOLO Object Detection",
104
- description="Upload an image or video, and the model will automatically detect objects using the YOLO model. The confidence and IOU thresholds are fixed and cannot be adjusted.",
105
- flagging_mode='never' # Use flagging_mode as never
106
- )
107
-
108
- # Launch the Gradio app with a public link
109
- gui.launch()