turhancan97 commited on
Commit
cfc7a55
1 Parent(s): 6f36913
Files changed (3) hide show
  1. .gitignore +7 -0
  2. app.py +91 -133
  3. requirements.txt +47 -5
.gitignore ADDED
@@ -0,0 +1,7 @@
 
 
 
 
 
 
 
 
1
+ flagged/
2
+ *.pt
3
+ *.png
4
+ *.jpg
5
+ *.mp4
6
+ *.mkv
7
+ gradio_cached_examples/
app.py CHANGED
@@ -1,146 +1,104 @@
1
  import gradio as gr
2
- import torch
3
- from sahi.prediction import ObjectPrediction
4
- from sahi.utils.cv import visualize_object_predictions, read_image
5
- from ultralytics import YOLO
6
  import cv2
7
- import numpy as np
8
- from math import atan2, cos, sin, sqrt, pi
9
 
10
- # Images
11
- torch.hub.download_url_to_file('https://github.com/lucarei/orientation-detection-robotic-grasping/assets/22428774/cefd9731-c57c-428b-b401-fd54a8bd0a95', 'highway.jpg')
12
- torch.hub.download_url_to_file('https://github.com/lucarei/orientation-detection-robotic-grasping/assets/22428774/acbad76a-33f9-4028-b012-4ece5998c272', 'highway1.jpg')
13
- torch.hub.download_url_to_file('https://github.com/lucarei/orientation-detection-robotic-grasping/assets/22428774/7fa95f52-3c8b-4ea0-8bca-7374792a4c55', 'small-vehicles1.jpeg')
14
 
15
- # def drawAxis(img, p_, q_, color, scale):
16
- # p = list(p_)
17
- # q = list(q_)
18
-
19
- # ## [visualization1]
20
- # angle = atan2(p[1] - q[1], p[0] - q[0]) # angle in radians
21
- # hypotenuse = sqrt((p[1] - q[1]) * (p[1] - q[1]) + (p[0] - q[0]) * (p[0] - q[0]))
22
-
23
- # # Here we lengthen the arrow by a factor of scale
24
- # q[0] = p[0] - scale * hypotenuse * cos(angle)
25
- # q[1] = p[1] - scale * hypotenuse * sin(angle)
26
- # cv2.line(img, (int(p[0]), int(p[1])), (int(q[0]), int(q[1])), color, 3, cv2.LINE_AA)
27
-
28
- # # create the arrow hooks
29
- # p[0] = q[0] + 9 * cos(angle + pi / 4)
30
- # p[1] = q[1] + 9 * sin(angle + pi / 4)
31
- # cv2.line(img, (int(p[0]), int(p[1])), (int(q[0]), int(q[1])), color, 3, cv2.LINE_AA)
32
-
33
- # p[0] = q[0] + 9 * cos(angle - pi / 4)
34
- # p[1] = q[1] + 9 * sin(angle - pi / 4)
35
- # cv2.line(img, (int(p[0]), int(p[1])), (int(q[0]), int(q[1])), color, 3, cv2.LINE_AA)
36
- # ## [visualization1]
37
-
38
 
39
- # def getOrientation(pts, img):
40
- # ## [pca]
41
- # # Construct a buffer used by the pca analysis
42
- # sz = len(pts)
43
- # data_pts = np.empty((sz, 2), dtype=np.float64)
44
- # for i in range(data_pts.shape[0]):
45
- # data_pts[i,0] = pts[i,0,0]
46
- # data_pts[i,1] = pts[i,0,1]
47
-
48
- # # Perform PCA analysis
49
- # mean = np.empty((0))
50
- # mean, eigenvectors, eigenvalues = cv2.PCACompute2(data_pts, mean)
51
-
52
- # # Store the center of the object
53
- # cntr = (int(mean[0,0]), int(mean[0,1]))
54
- # ## [pca]
55
-
56
- # ## [visualization]
57
- # # Draw the principal components
58
- # cv2.circle(img, cntr, 3, (255, 0, 255), 10)
59
- # p1 = (cntr[0] + 0.02 * eigenvectors[0,0] * eigenvalues[0,0], cntr[1] + 0.02 * eigenvectors[0,1] * eigenvalues[0,0])
60
- # p2 = (cntr[0] - 0.02 * eigenvectors[1,0] * eigenvalues[1,0], cntr[1] - 0.02 * eigenvectors[1,1] * eigenvalues[1,0])
61
- # drawAxis(img, cntr, p1, (255, 255, 0), 1)
62
- # drawAxis(img, cntr, p2, (0, 0, 255), 3)
63
-
64
- # angle = atan2(eigenvectors[0,1], eigenvectors[0,0]) # orientation in radians
65
- # ## [visualization]
66
- # angle_deg = -(int(np.rad2deg(angle))-180) % 180
67
-
68
- # # Label with the rotation angle
69
- # label = " Rotation Angle: " + str(int(np.rad2deg(angle))) + " degrees"
70
- # textbox = cv2.rectangle(img, (cntr[0], cntr[1]-25), (cntr[0] + 250, cntr[1] + 10), (255,255,255), -1)
71
- # cv2.putText(img, label, (cntr[0], cntr[1]), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0,0,0), 1, cv2.LINE_AA)
72
 
73
- # return angle_deg
 
 
 
 
 
 
 
 
 
 
74
 
75
- def yolov8_inference(
76
- image: gr.inputs.Image = None,
77
- model_path: gr.inputs.Dropdown = None,
78
- image_size: gr.inputs.Slider = 640,
79
- conf_threshold: gr.inputs.Slider = 0.25,
80
- iou_threshold: gr.inputs.Slider = 0.45,
81
- ):
82
- """
83
- YOLOv8 inference function
84
- Args:
85
- image: Input image
86
- model_path: Path to the model
87
- image_size: Image size
88
- conf_threshold: Confidence threshold
89
- iou_threshold: IOU threshold
90
- Returns:
91
- Rendered image
92
- """
93
- model = YOLO(model_path)
94
- model.conf = conf_threshold
95
- model.iou = iou_threshold
96
- results = model.predict(image, imgsz=image_size, return_outputs=True)
97
- object_prediction_list = []
98
- for _, image_results in enumerate(results):
99
- if len(image_results)!=0:
100
- image_predictions_in_xyxy_format = image_results['det']
101
- for pred in image_predictions_in_xyxy_format:
102
- x1, y1, x2, y2 = (
103
- int(pred[0]),
104
- int(pred[1]),
105
- int(pred[2]),
106
- int(pred[3]),
107
- )
108
- bbox = [x1, y1, x2, y2]
109
- score = pred[4]
110
- category_name = model.model.names[int(pred[5])]
111
- category_id = pred[5]
112
- object_prediction = ObjectPrediction(
113
- bbox=bbox,
114
- category_id=int(category_id),
115
- score=score,
116
- category_name=category_name,
117
- )
118
- object_prediction_list.append(object_prediction)
119
 
120
- image = read_image(image)
121
- output_image = visualize_object_predictions(image=image, object_prediction_list=object_prediction_list)
122
- return output_image['image']
 
 
 
 
 
 
 
 
 
 
 
123
 
124
- inputs = [
125
- gr.inputs.Image(type="filepath", label="Input Image"),
126
- gr.inputs.Dropdown(["turhancan97/yolov8m-trash"],
127
- default="turhancan97/yolov8m-trash", label="Model"),
128
- gr.inputs.Slider(minimum=320, maximum=1280, default=640, step=32, label="Image Size"),
129
- gr.inputs.Slider(minimum=0.0, maximum=1.0, default=0.25, step=0.05, label="Confidence Threshold"),
130
- gr.inputs.Slider(minimum=0.0, maximum=1.0, default=0.45, step=0.05, label="IOU Threshold"),
131
  ]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
132
 
133
- outputs = gr.outputs.Image(type="filepath", label="Output Image")
134
- title = "Ultralytics YOLOv8: State-of-the-Art YOLO Models"
135
 
136
- examples = [['highway.jpg', 'turhancan97/yolov8m-trash', 640, 0.25, 0.45], ['highway1.jpg', 'turhancan97/yolov8m-trash', 640, 0.25, 0.45], ['small-vehicles1.jpeg', 'turhancan97/yolov8m-trash', 1280, 0.25, 0.45]]
137
- demo_app = gr.Interface(
138
- fn=yolov8_inference,
139
- inputs=inputs,
140
- outputs=outputs,
141
- title=title,
142
- examples=examples,
143
- cache_examples=True,
144
- theme='huggingface',
 
 
145
  )
146
- demo_app.launch(debug=True, enable_queue=True)
 
 
 
 
 
1
  import gradio as gr
 
 
 
 
2
  import cv2
3
+ import requests
4
+ import os
5
 
6
+ from ultralytics import YOLO
 
 
 
7
 
8
+ file_urls = [
9
+ 'https://github.com/lucarei/orientation-detection-robotic-grasping/assets/22428774/cefd9731-c57c-428b-b401-fd54a8bd0a95',
10
+ 'https://github.com/lucarei/orientation-detection-robotic-grasping/assets/22428774/acbad76a-33f9-4028-b012-4ece5998c272',
11
+ 'https://www.dropbox.com/s/7sjfwncffg8xej2/video_7.mp4?dl=1'
12
+ ]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
13
 
14
+ def download_file(url, save_name):
15
+ url = url
16
+ if not os.path.exists(save_name):
17
+ file = requests.get(url)
18
+ open(save_name, 'wb').write(file.content)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
19
 
20
+ for i, url in enumerate(file_urls):
21
+ if 'mp4' in file_urls[i]:
22
+ download_file(
23
+ file_urls[i],
24
+ f"video.mp4"
25
+ )
26
+ else:
27
+ download_file(
28
+ file_urls[i],
29
+ f"image_{i}.jpg"
30
+ )
31
 
32
+ model = YOLO('best.pt')
33
+ path = [['image_0.jpg'], ['image_1.jpg']]
34
+ video_path = [['video.mp4']]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
35
 
36
+ def show_preds_image(image_path):
37
+ image = cv2.imread(image_path)
38
+ outputs = model.predict(source=image_path)
39
+ results = outputs[0].cpu().numpy()
40
+ for i, det in enumerate(results.boxes.xyxy):
41
+ cv2.rectangle(
42
+ image,
43
+ (int(det[0]), int(det[1])),
44
+ (int(det[2]), int(det[3])),
45
+ color=(0, 0, 255),
46
+ thickness=2,
47
+ lineType=cv2.LINE_AA
48
+ )
49
+ return cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
50
 
51
+ inputs_image = [
52
+ gr.components.Image(type="filepath", label="Input Image"),
53
+ ]
54
+ outputs_image = [
55
+ gr.components.Image(type="numpy", label="Output Image"),
 
 
56
  ]
57
+ interface_image = gr.Interface(
58
+ fn=show_preds_image,
59
+ inputs=inputs_image,
60
+ outputs=outputs_image,
61
+ title="Pothole detector app",
62
+ examples=path,
63
+ cache_examples=False,
64
+ )
65
+
66
+ def show_preds_video(video_path):
67
+ cap = cv2.VideoCapture(video_path)
68
+ while(cap.isOpened()):
69
+ ret, frame = cap.read()
70
+ if ret:
71
+ frame_copy = frame.copy()
72
+ outputs = model.predict(source=frame)
73
+ results = outputs[0].cpu().numpy()
74
+ for i, det in enumerate(results.boxes.xyxy):
75
+ cv2.rectangle(
76
+ frame_copy,
77
+ (int(det[0]), int(det[1])),
78
+ (int(det[2]), int(det[3])),
79
+ color=(0, 0, 255),
80
+ thickness=2,
81
+ lineType=cv2.LINE_AA
82
+ )
83
+ yield cv2.cvtColor(frame_copy, cv2.COLOR_BGR2RGB)
84
 
85
+ inputs_video = [
86
+ gr.components.Video(type="filepath", label="Input Video"),
87
 
88
+ ]
89
+ outputs_video = [
90
+ gr.components.Image(type="numpy", label="Output Image"),
91
+ ]
92
+ interface_video = gr.Interface(
93
+ fn=show_preds_video,
94
+ inputs=inputs_video,
95
+ outputs=outputs_video,
96
+ title="Pothole detector",
97
+ examples=video_path,
98
+ cache_examples=False,
99
  )
100
+
101
+ gr.TabbedInterface(
102
+ [interface_image, interface_video],
103
+ tab_names=['Image inference', 'Video inference']
104
+ ).queue().launch()
requirements.txt CHANGED
@@ -1,5 +1,47 @@
1
- opencv_python
2
- torch
3
- sahi
4
- ultralytics==8.0.4
5
- ultralyticsplus==0.0.3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+
18
+ # Logging -------------------------------------
19
+ tensorboard>=2.4.1
20
+ # clearml
21
+ # comet
22
+
23
+ # Plotting ------------------------------------
24
+ pandas>=1.1.4
25
+ seaborn>=0.11.0
26
+
27
+ # Export --------------------------------------
28
+ # coremltools>=6.0 # CoreML export
29
+ # onnx>=1.12.0 # ONNX export
30
+ # onnx-simplifier>=0.4.1 # ONNX simplifier
31
+ # nvidia-pyindex # TensorRT export
32
+ # nvidia-tensorrt # TensorRT export
33
+ # scikit-learn==0.19.2 # CoreML quantization
34
+ # tensorflow>=2.4.1 # TF exports (-cpu, -aarch64, -macos)
35
+ # tensorflowjs>=3.9.0 # TF.js export
36
+ # openvino-dev # OpenVINO export
37
+
38
+ # Extras --------------------------------------
39
+ ipython # interactive notebook
40
+ psutil # system utilization
41
+ thop>=0.1.1 # FLOPs computation
42
+ # albumentations>=1.0.3
43
+ # pycocotools>=2.0.6 # COCO mAP
44
+ # roboflow
45
+
46
+ # HUB -----------------------------------------
47
+ GitPython>=3.1.24