Spaces:
Running
Running
turhancan97
commited on
Commit
•
c53d901
1
Parent(s):
3038cac
add orientation
Browse files
app.py
CHANGED
@@ -2,9 +2,70 @@ 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',
|
@@ -35,18 +96,45 @@ 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 |
-
|
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(
|
50 |
|
51 |
inputs_image = [
|
52 |
gr.components.Image(type="filepath", label="Input Image"),
|
|
|
2 |
import cv2
|
3 |
import requests
|
4 |
import os
|
5 |
+
import numpy as np
|
6 |
+
from math import atan2, cos, sin, sqrt, pi
|
7 |
from ultralytics import YOLO
|
8 |
|
9 |
+
def drawAxis(img, p_, q_, color, scale):
|
10 |
+
p = list(p_)
|
11 |
+
q = list(q_)
|
12 |
+
|
13 |
+
## [visualization1]
|
14 |
+
angle = atan2(p[1] - q[1], p[0] - q[0]) # angle in radians
|
15 |
+
hypotenuse = sqrt((p[1] - q[1]) * (p[1] - q[1]) + (p[0] - q[0]) * (p[0] - q[0]))
|
16 |
+
|
17 |
+
# Here we lengthen the arrow by a factor of scale
|
18 |
+
q[0] = p[0] - scale * hypotenuse * cos(angle)
|
19 |
+
q[1] = p[1] - scale * hypotenuse * sin(angle)
|
20 |
+
cv2.line(img, (int(p[0]), int(p[1])), (int(q[0]), int(q[1])), color, 3, cv2.LINE_AA)
|
21 |
+
|
22 |
+
# create the arrow hooks
|
23 |
+
p[0] = q[0] + 9 * cos(angle + pi / 4)
|
24 |
+
p[1] = q[1] + 9 * sin(angle + pi / 4)
|
25 |
+
cv2.line(img, (int(p[0]), int(p[1])), (int(q[0]), int(q[1])), color, 3, cv2.LINE_AA)
|
26 |
+
|
27 |
+
p[0] = q[0] + 9 * cos(angle - pi / 4)
|
28 |
+
p[1] = q[1] + 9 * sin(angle - pi / 4)
|
29 |
+
cv2.line(img, (int(p[0]), int(p[1])), (int(q[0]), int(q[1])), color, 3, cv2.LINE_AA)
|
30 |
+
## [visualization1]
|
31 |
+
|
32 |
+
|
33 |
+
def getOrientation(pts, img):
|
34 |
+
## [pca]
|
35 |
+
# Construct a buffer used by the pca analysis
|
36 |
+
sz = len(pts)
|
37 |
+
data_pts = np.empty((sz, 2), dtype=np.float64)
|
38 |
+
for i in range(data_pts.shape[0]):
|
39 |
+
data_pts[i,0] = pts[i,0,0]
|
40 |
+
data_pts[i,1] = pts[i,0,1]
|
41 |
+
|
42 |
+
# Perform PCA analysis
|
43 |
+
mean = np.empty((0))
|
44 |
+
mean, eigenvectors, eigenvalues = cv2.PCACompute2(data_pts, mean)
|
45 |
+
|
46 |
+
# Store the center of the object
|
47 |
+
cntr = (int(mean[0,0]), int(mean[0,1]))
|
48 |
+
## [pca]
|
49 |
+
|
50 |
+
## [visualization]
|
51 |
+
# Draw the principal components
|
52 |
+
cv2.circle(img, cntr, 3, (255, 0, 255), 10)
|
53 |
+
p1 = (cntr[0] + 0.02 * eigenvectors[0,0] * eigenvalues[0,0], cntr[1] + 0.02 * eigenvectors[0,1] * eigenvalues[0,0])
|
54 |
+
p2 = (cntr[0] - 0.02 * eigenvectors[1,0] * eigenvalues[1,0], cntr[1] - 0.02 * eigenvectors[1,1] * eigenvalues[1,0])
|
55 |
+
drawAxis(img, cntr, p1, (255, 255, 0), 1)
|
56 |
+
drawAxis(img, cntr, p2, (0, 0, 255), 3)
|
57 |
+
|
58 |
+
angle = atan2(eigenvectors[0,1], eigenvectors[0,0]) # orientation in radians
|
59 |
+
## [visualization]
|
60 |
+
angle_deg = -(int(np.rad2deg(angle))-180) % 180
|
61 |
+
|
62 |
+
# Label with the rotation angle
|
63 |
+
label = " Rotation Angle: " + str(int(np.rad2deg(angle))) + " degrees"
|
64 |
+
textbox = cv2.rectangle(img, (cntr[0], cntr[1]-25), (cntr[0] + 250, cntr[1] + 10), (255,255,255), -1)
|
65 |
+
cv2.putText(img, label, (cntr[0], cntr[1]), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0,0,0), 1, cv2.LINE_AA)
|
66 |
+
|
67 |
+
return angle_deg
|
68 |
+
|
69 |
file_urls = [
|
70 |
'https://github.com/lucarei/orientation-detection-robotic-grasping/assets/22428774/cefd9731-c57c-428b-b401-fd54a8bd0a95',
|
71 |
'https://github.com/lucarei/orientation-detection-robotic-grasping/assets/22428774/acbad76a-33f9-4028-b012-4ece5998c272',
|
|
|
96 |
|
97 |
def show_preds_image(image_path):
|
98 |
image = cv2.imread(image_path)
|
99 |
+
#resize image (optional)
|
100 |
+
img_res_toshow = cv2.resize(image, None, fx= 0.5, fy= 0.5, interpolation= cv2.INTER_LINEAR)
|
101 |
+
height=img_res_toshow.shape[0]
|
102 |
+
width=img_res_toshow.shape[1]
|
103 |
+
dim=(width,height)
|
104 |
+
|
105 |
outputs = model.predict(source=image_path)
|
106 |
+
|
107 |
+
#obtain BW image
|
108 |
+
bw=(outputs[0].masks.masks[0].cpu().numpy() * 255).astype("uint8")
|
109 |
+
#BW image with same dimention of initial image
|
110 |
+
bw=cv2.resize(bw, dim, interpolation = cv2.INTER_AREA)
|
111 |
+
img=img_res_toshow
|
112 |
+
contours, _ = cv2.findContours(bw, cv2.RETR_LIST, cv2.CHAIN_APPROX_NONE)
|
113 |
+
for i, c in enumerate(contours):
|
114 |
+
# Calculate the area of each contour
|
115 |
+
area = cv2.contourArea(c)
|
116 |
+
|
117 |
+
# Ignore contours that are too small or too large
|
118 |
+
if area < 3700 or 100000 < area:
|
119 |
+
continue
|
120 |
+
|
121 |
+
# Draw each contour only for visualisation purposes
|
122 |
+
cv2.drawContours(img, contours, i, (0, 0, 255), 2)
|
123 |
+
|
124 |
+
# Find the orientation of each shape
|
125 |
+
angle_deg = getOrientation(c, img)
|
126 |
+
|
127 |
results = outputs[0].cpu().numpy()
|
128 |
for i, det in enumerate(results.boxes.xyxy):
|
129 |
cv2.rectangle(
|
130 |
+
img,
|
131 |
(int(det[0]), int(det[1])),
|
132 |
(int(det[2]), int(det[3])),
|
133 |
color=(0, 0, 255),
|
134 |
thickness=2,
|
135 |
lineType=cv2.LINE_AA
|
136 |
)
|
137 |
+
return cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
|
138 |
|
139 |
inputs_image = [
|
140 |
gr.components.Image(type="filepath", label="Input Image"),
|