Upload 3 files
Browse files- app.py +169 -0
- requirements.txt +49 -0
- yolov8x-seg.pt +3 -0
app.py
ADDED
@@ -0,0 +1,169 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import cv2
|
3 |
+
import requests
|
4 |
+
import os
|
5 |
+
import cvzone
|
6 |
+
import math
|
7 |
+
import random
|
8 |
+
import numpy as np
|
9 |
+
|
10 |
+
from ultralytics import YOLO
|
11 |
+
|
12 |
+
|
13 |
+
|
14 |
+
|
15 |
+
model = YOLO('yolov8x-seg.pt')
|
16 |
+
path = []
|
17 |
+
video_path = []
|
18 |
+
|
19 |
+
listClasses = ['person', 'bicycle', 'car']
|
20 |
+
|
21 |
+
|
22 |
+
def show_preds_image(image_path):
|
23 |
+
image = cv2.imread(image_path)
|
24 |
+
outputs = model.predict(source=image_path)
|
25 |
+
results = outputs[0].cpu().numpy()
|
26 |
+
|
27 |
+
yolo_classes = list(model.names.values())
|
28 |
+
classes_ids = [yolo_classes.index(clas) for clas in yolo_classes]
|
29 |
+
colors = [random.choices(range(256), k=3) for _ in classes_ids]
|
30 |
+
|
31 |
+
for result in outputs:
|
32 |
+
for mask, box in zip(result.masks.xy, result.boxes):
|
33 |
+
|
34 |
+
#for r in results:
|
35 |
+
#boxes = r.boxes
|
36 |
+
#for box in boxes:
|
37 |
+
cls = box.cls[0]
|
38 |
+
conf = math.ceil((box.conf[0]*100))/100
|
39 |
+
if (int(cls)<3) and (conf > 0.70):
|
40 |
+
|
41 |
+
points = np.int32([mask])
|
42 |
+
# cv2.polylines(img, points, True, (255, 0, 0), 1)
|
43 |
+
color_number = classes_ids.index(int(box.cls[0]))
|
44 |
+
color = colors[color_number]
|
45 |
+
|
46 |
+
cv2.fillPoly(image, points, color)
|
47 |
+
|
48 |
+
x1, y1, x2, y2 = box.xyxy[0]
|
49 |
+
x1, y1, x2, y2 = int(x1), int(y1), int(x2), int(y2)
|
50 |
+
|
51 |
+
cv2.rectangle(
|
52 |
+
image,
|
53 |
+
(int(x1), int(y1), int(x2), int(y2)),
|
54 |
+
color=(0, 0, 255),
|
55 |
+
thickness=2,
|
56 |
+
lineType=cv2.LINE_AA
|
57 |
+
)
|
58 |
+
|
59 |
+
|
60 |
+
name = yolo_classes[int(cls)]
|
61 |
+
# fontScale
|
62 |
+
fontScale = 0.5
|
63 |
+
|
64 |
+
color_number = classes_ids.index(int(box.cls[0]))
|
65 |
+
color = colors[color_number]
|
66 |
+
|
67 |
+
# Line thickness of 2 px
|
68 |
+
thickness = 1
|
69 |
+
font = cv2.FONT_HERSHEY_SIMPLEX
|
70 |
+
|
71 |
+
cv2.putText(image, str(name) + " " + str(conf), (max(0,x1), max(35,y1)), font,
|
72 |
+
fontScale, color, thickness, cv2.LINE_AA)
|
73 |
+
|
74 |
+
|
75 |
+
return cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
|
76 |
+
|
77 |
+
def show_preds_webcam(pil_image):
|
78 |
+
image= np.array(pil_image)
|
79 |
+
outputs = model.predict(image)
|
80 |
+
results = outputs[0].cpu().numpy()
|
81 |
+
|
82 |
+
yolo_classes = list(model.names.values())
|
83 |
+
classes_ids = [yolo_classes.index(clas) for clas in yolo_classes]
|
84 |
+
colors = [random.choices(range(256), k=3) for _ in classes_ids]
|
85 |
+
|
86 |
+
for result in outputs:
|
87 |
+
for mask, box in zip(result.masks.xy, result.boxes):
|
88 |
+
|
89 |
+
#for r in results:
|
90 |
+
#boxes = r.boxes
|
91 |
+
#for box in boxes:
|
92 |
+
cls = box.cls[0]
|
93 |
+
conf = math.ceil((box.conf[0]*100))/100
|
94 |
+
if (int(cls)<3) and (conf > 0.70):
|
95 |
+
|
96 |
+
points = np.int32([mask])
|
97 |
+
# cv2.polylines(img, points, True, (255, 0, 0), 1)
|
98 |
+
color_number = classes_ids.index(int(box.cls[0]))
|
99 |
+
color = colors[color_number]
|
100 |
+
|
101 |
+
cv2.fillPoly(image, points, color)
|
102 |
+
|
103 |
+
x1, y1, x2, y2 = box.xyxy[0]
|
104 |
+
x1, y1, x2, y2 = int(x1), int(y1), int(x2), int(y2)
|
105 |
+
|
106 |
+
cv2.rectangle(
|
107 |
+
image,
|
108 |
+
(int(x1), int(y1), int(x2), int(y2)),
|
109 |
+
color=(0, 0, 255),
|
110 |
+
thickness=2,
|
111 |
+
lineType=cv2.LINE_AA
|
112 |
+
)
|
113 |
+
|
114 |
+
|
115 |
+
name = yolo_classes[int(cls)]
|
116 |
+
# fontScale
|
117 |
+
fontScale = 0.5
|
118 |
+
|
119 |
+
color_number = classes_ids.index(int(box.cls[0]))
|
120 |
+
color = colors[color_number]
|
121 |
+
|
122 |
+
# Line thickness of 2 px
|
123 |
+
thickness = 1
|
124 |
+
font = cv2.FONT_HERSHEY_SIMPLEX
|
125 |
+
|
126 |
+
cv2.putText(image, str(name) + " " + str(conf), (max(0,x1), max(35,y1)), font,
|
127 |
+
fontScale, color, thickness, cv2.LINE_AA)
|
128 |
+
|
129 |
+
|
130 |
+
return image
|
131 |
+
|
132 |
+
|
133 |
+
|
134 |
+
inputs_image = [
|
135 |
+
gr.components.Image(type="filepath", label="Input Image"),
|
136 |
+
]
|
137 |
+
outputs_image = [
|
138 |
+
gr.components.Image(type="numpy", label="Output Image"),
|
139 |
+
]
|
140 |
+
interface_image = gr.Interface(
|
141 |
+
fn=show_preds_image,
|
142 |
+
inputs=inputs_image,
|
143 |
+
outputs=outputs_image,
|
144 |
+
title="Object segmentation",
|
145 |
+
examples=path,
|
146 |
+
cache_examples=False,
|
147 |
+
)
|
148 |
+
|
149 |
+
|
150 |
+
|
151 |
+
|
152 |
+
outputs_video = [
|
153 |
+
gr.components.Image(type="numpy", label="Output Image"),
|
154 |
+
]
|
155 |
+
|
156 |
+
|
157 |
+
interface_webcam = gr.Interface(
|
158 |
+
fn=show_preds_webcam,
|
159 |
+
live=True,
|
160 |
+
inputs=gr.Image(source="webcam", streaming=True, type="pil"),
|
161 |
+
outputs=outputs_video,
|
162 |
+
)
|
163 |
+
|
164 |
+
gr.TabbedInterface(
|
165 |
+
[ interface_webcam, interface_image],
|
166 |
+
tab_names=[ 'Webcam', "Image"]
|
167 |
+
).queue().launch()
|
168 |
+
|
169 |
+
|
requirements.txt
ADDED
@@ -0,0 +1,49 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Ultralytics requirements
|
2 |
+
# Usage: pip install -r requirements.txt
|
3 |
+
|
4 |
+
# Base ----------------------------------------
|
5 |
+
hydra-core>=1.2.0
|
6 |
+
matplotlib>=3.2.2
|
7 |
+
numpy>=1.18.5
|
8 |
+
opencv-python>=4.1.1
|
9 |
+
Pillow>=7.1.2
|
10 |
+
PyYAML>=5.3.1
|
11 |
+
requests>=2.23.0
|
12 |
+
scipy>=1.4.1
|
13 |
+
torch>=1.7.0
|
14 |
+
torchvision>=0.8.1
|
15 |
+
tqdm>=4.64.0
|
16 |
+
ultralytics
|
17 |
+
cvzone
|
18 |
+
python-math
|
19 |
+
|
20 |
+
# Logging -------------------------------------
|
21 |
+
tensorboard>=2.4.1
|
22 |
+
# clearml
|
23 |
+
# comet
|
24 |
+
|
25 |
+
# Plotting ------------------------------------
|
26 |
+
pandas>=1.1.4
|
27 |
+
seaborn>=0.11.0
|
28 |
+
|
29 |
+
# Export --------------------------------------
|
30 |
+
# coremltools>=6.0 # CoreML export
|
31 |
+
# onnx>=1.12.0 # ONNX export
|
32 |
+
# onnx-simplifier>=0.4.1 # ONNX simplifier
|
33 |
+
# nvidia-pyindex # TensorRT export
|
34 |
+
# nvidia-tensorrt # TensorRT export
|
35 |
+
# scikit-learn==0.19.2 # CoreML quantization
|
36 |
+
# tensorflow>=2.4.1 # TF exports (-cpu, -aarch64, -macos)
|
37 |
+
# tensorflowjs>=3.9.0 # TF.js export
|
38 |
+
# openvino-dev # OpenVINO export
|
39 |
+
|
40 |
+
# Extras --------------------------------------
|
41 |
+
ipython # interactive notebook
|
42 |
+
psutil # system utilization
|
43 |
+
thop>=0.1.1 # FLOPs computation
|
44 |
+
# albumentations>=1.0.3
|
45 |
+
# pycocotools>=2.0.6 # COCO mAP
|
46 |
+
# roboflow
|
47 |
+
|
48 |
+
# HUB -----------------------------------------
|
49 |
+
GitPython>=3.1.24
|
yolov8x-seg.pt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:d63cbfa5764867c0066bedfa43cf2dcd90a412a1de44b2e238c43978a9d28ea6
|
3 |
+
size 144076467
|