-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimagedetection.py
More file actions
108 lines (94 loc) · 3.46 KB
/
imagedetection.py
File metadata and controls
108 lines (94 loc) · 3.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
import cv2
import numpy as np
from picamera2 import Picamera2
cam0 = Picamera2(0)
cam1 = Picamera2(1)
height = 600
width = 800
middle = (int(width / 2), int(height / 2))
cam0.configure(cam0.create_video_configuration(main={"format": 'RGB888', "size": (width, height)}))
cam0.start()
cam1.configure(cam0.create_video_configuration(main={"format": 'RGB888', "size": (width, height)}))
cam1.start()
lower_range=np.array([0,74,178])
upper_range=np.array([179,255,255])
Known_distance=1
Known_width=1
def Focal_Length_Finder(Known_distance, real_width, width_in_rf_image):
focal_length = (width_in_rf_image * Known_distance) / real_width
return focal_length
def obj_data(img):
obj_pos = (0,0)
hsv=cv2.cvtColor(img,cv2.COLOR_BGR2HSV)
mask=cv2.inRange(hsv,lower_range,upper_range)
_,mask1=cv2.threshold(mask,254,255,cv2.THRESH_BINARY)
cnts,_=cv2.findContours(mask1,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_NONE)
for c in cnts:
x=600
if cv2.contourArea(c)>x:
x,y,w,h=cv2.boundingRect(c)
cv2.rectangle(img,(x,y),(x+w,y+h),(0,255,0),2)
obj_pos = (x,y)
return obj_pos
# while True:
# print("yo")
# width = 800
# height= 600
#
# pointx = 400
# pointy = 100
#
# centerx = 400
# centery = 300
# frame0=cam0.capture_array()
# frame0=cv2.resize(frame0,(width,height))
# cv2.line(frame0, (pointx, pointy), (pointx, pointy), (0, 0, 255), 10)
# cv2.line(frame0, (centerx,centery), (centerx,centery), (0, 255, 0), 10)
# obj_width_in_frame0=obj_data(frame0)
# if obj_width_in_frame0[0] != 0:
# x=obj_width_in_frame0[0]
# y=obj_width_in_frame0[1]
# cv2.putText(frame0, f"Position: {x}, {y}", (30, 35),cv2.FONT_HERSHEY_COMPLEX, 0.6, (255,0,0), 2)
#
# frame1=cam1.capture_array()
# frame1=cv2.resize(frame1,(width,height))
# cv2.line(frame1, (centerx,centery), (centerx,centery), (0, 255, 0), 10)
# cv2.line(frame1, (pointx, pointy), (pointx, pointy), (0, 0, 255), 10)
# obj_width_in_frame1=obj_data(frame1)
# if obj_width_in_frame1[0] != 0:
# x=obj_width_in_frame1[0]
# y=obj_width_in_frame1[1]
# cv2.putText(frame1, f"Position: {x}, {y}", (30, 35),cv2.FONT_HERSHEY_COMPLEX, 0.6, (255,0,0), 2)
#
#
# cv2.imshow("Right",frame0)
# cv2.imshow("Left",frame1)
# if cv2.waitKey(1)&0xFF==27:
# break
cv2.destroyAllWindows()
# cap.release()
# cv2.destroyAllWindows()
def camerasGetTargetPixel():
frame0=cam0.capture_array()
frame0=cv2.resize(frame0,(width,height))
# obj_width_in_frame0=obj_data(frame0)
# if obj_width_in_frame0[0] != 0:
# x=obj_width_in_frame0[0]
# y=obj_width_in_frame0[1]
# cv2.putText(frame0, f"Position: {x}, {y}", (30, 35),cv2.FONT_HERSHEY_COMPLEX, 0.6, (255,0,0), 2)
# frame1=cam1.capture_array()
# frame1=cv2.resize(frame1,(width,height))
# #cv2.line(frame1, (centerx,centery), (centerx,centery), (0, 255, 0), 10)
# #cv2.line(frame1, (pointx, pointy), (pointx, pointy), (0, 0, 255), 10)
# obj_width_in_frame1=obj_data(frame1)
# if obj_width_in_frame1[0] != 0:
# x=obj_width_in_frame1[0]
# y=obj_width_in_frame1[1]
# cv2.putText(frame1, f"Position: {x}, {y}", (30, 35),cv2.FONT_HERSHEY_COMPLEX, 0.6, (255,0,0), 2)
# cv2.imshow("Right",frame0)
# cv2.imshow("Left",frame1)
# if cv2.waitKey(1)&0xFF==27:
# break
# cap.release()
# cv2.destroyAllWindows()
return height