turhancan97 commited on
Commit
558e4c3
1 Parent(s): b548adc

app deploy

Browse files
Files changed (1) hide show
  1. app.py +22 -11
app.py CHANGED
@@ -17,16 +17,16 @@ def drawAxis(img, p_, q_, color, scale):
17
  # Here we lengthen the arrow by a factor of scale
18
  q[0] = p[0] - scale * hypotenuse/3 * cos(angle)
19
  q[1] = p[1] - scale * hypotenuse/3 * 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
 
@@ -52,17 +52,17 @@ def getOrientation(pts, img):
52
  cv2.circle(img, cntr, 3, (255, 0, 255), 15)
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 = str(int(np.rad2deg(angle))) + " deg"
64
- textbox = cv2.rectangle(img, (cntr[0]+60, cntr[1]-25), (cntr[0] + 150, cntr[1] + 10), (255,255,255), -1)
65
- cv2.putText(img, label, (cntr[0]+60, cntr[1]), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0,0,0), 1, cv2.LINE_AA)
66
 
67
  return angle_deg
68
 
@@ -92,10 +92,11 @@ for i, url in enumerate(file_urls):
92
  )
93
 
94
  model = YOLO('best.pt')
95
- path = [['image_0.jpg'], ['image_1.jpg']]
96
  video_path = [['video.mp4']]
97
 
98
  def show_preds_image(image_path):
 
99
  image = cv2.imread(image_path)
100
  #resize image (optional)
101
  img_res_toshow = cv2.resize(image, None, fx= 0.5, fy= 0.5, interpolation= cv2.INTER_LINEAR)
@@ -123,7 +124,7 @@ def show_preds_image(image_path):
123
  cv2.drawContours(img, contours, i, (0, 0, 255), 2)
124
 
125
  # Find the orientation of each shape
126
- angle_deg = getOrientation(c, img)
127
 
128
  results = outputs[0].cpu().numpy()
129
  for i, det in enumerate(results.boxes.xyxy):
@@ -152,6 +153,16 @@ interface_image = gr.Interface(
152
  cache_examples=False,
153
  )
154
 
 
 
 
 
 
 
 
 
 
 
155
  def show_preds_video(video_path):
156
  cap = cv2.VideoCapture(video_path)
157
  while(cap.isOpened()):
 
17
  # Here we lengthen the arrow by a factor of scale
18
  q[0] = p[0] - scale * hypotenuse/3 * cos(angle)
19
  q[1] = p[1] - scale * hypotenuse/3 * sin(angle)
20
+ cv2.line(img, (int(p[0]), int(p[1])), (int(q[0]), int(q[1])), color, 4, 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, 4, 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, 4, cv2.LINE_AA)
30
  ## [visualization1]
31
 
32
 
 
52
  cv2.circle(img, cntr, 3, (255, 0, 255), 15)
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, (0, 0, 0), 1)
56
+ drawAxis(img, cntr, p2, (0, 0, 0), 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 = str(int(np.rad2deg(angle))) + " deg"
64
+ # textbox = cv2.rectangle(img, (cntr[0]+60, cntr[1]-25), (cntr[0] + 150, cntr[1] + 10), (255,255,255), -1)
65
+ # cv2.putText(img, label, (cntr[0]+60, cntr[1]), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0,0,0), 1, cv2.LINE_AA)
66
 
67
  return angle_deg
68
 
 
92
  )
93
 
94
  model = YOLO('best.pt')
95
+ path = [['image_0.jpg'], ['image_1.jpg'], ['image_2.jpg']]
96
  video_path = [['video.mp4']]
97
 
98
  def show_preds_image(image_path):
99
+ global angle
100
  image = cv2.imread(image_path)
101
  #resize image (optional)
102
  img_res_toshow = cv2.resize(image, None, fx= 0.5, fy= 0.5, interpolation= cv2.INTER_LINEAR)
 
124
  cv2.drawContours(img, contours, i, (0, 0, 255), 2)
125
 
126
  # Find the orientation of each shape
127
+ angle = getOrientation(c, img)
128
 
129
  results = outputs[0].cpu().numpy()
130
  for i, det in enumerate(results.boxes.xyxy):
 
153
  cache_examples=False,
154
  )
155
 
156
+ def angle_deg():
157
+ global angle
158
+ return str(int(np.rad2deg(angle))) + " deg"
159
+
160
+
161
+ interface_image = gr.Interface(
162
+ fn=angle_deg,
163
+ outputs='text',
164
+ )
165
+
166
  def show_preds_video(video_path):
167
  cap = cv2.VideoCapture(video_path)
168
  while(cap.isOpened()):