-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcam.py
90 lines (65 loc) · 2.44 KB
/
cam.py
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
# Kamera Sensor Modul
import numpy as np
import cv2
import conf
cam = cv2.VideoCapture(0)
def lineDetection():
# Capture the frames
ret, img = cam.read()
# Crop the image
img = cv2.resize(img, (340, 220))
img = img[conf.camCropLT[0]:conf.camCropLT[1], conf.camCropLT[2]:conf.camCropLT[3]]
# Convert to grayscale
img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# Gaussian blur
blur = cv2.GaussianBlur(img_gray,(5,5),0)
# Color thresholding
ret,thresh = cv2.threshold(blur,60,255,cv2.THRESH_BINARY_INV)
# Find the contours of the frame
_,contours,h = cv2.findContours(thresh.copy(), 1, cv2.CHAIN_APPROX_NONE)
# Find the biggest contour (if detected)
if len(contours) > 0:
c = max(contours, key=cv2.contourArea)
M = cv2.moments(c)
try:
cx = int(M['m10']/M['m00'])
cy = int(M['m01']/M['m00'])
except ZeroDivisionError:
return False
cv2.line(img,(cx,0),(cx,720),(255,0,0),1)
cv2.line(img,(0,cy),(1280,cy),(255,0,0),1)
cv2.drawContours(img, contours, -1, (0,255,0), 1)
cdiff = int(cx-70)
linecoord = [cx,cy,cdiff]
if conf.debug:
print("LineDetection:{}".format(linecoord))
return linecoord
def objectDetection():
kernelOpen=np.ones((5,5))
kernelClose=np.ones((20,20))
lowerBound=np.array([conf.camColorLB[0],conf.camColorLB[1],conf.camColorLB[2]])
upperBound=np.array([conf.camColorUB[0],conf.camColorUB[1],conf.camColorUB[2]])
obstacles = dict()
ocount = 0
for j in range(9):
ret, img=cam.read()
img=cv2.resize(img,(340,220))
#convert BGR to HSV
imgHSV= cv2.cvtColor(img,cv2.COLOR_BGR2HSV)
# create the Mask
mask=cv2.inRange(imgHSV,lowerBound,upperBound)
#morphology
maskOpen=cv2.morphologyEx(mask,cv2.MORPH_OPEN,kernelOpen)
maskClose=cv2.morphologyEx(maskOpen,cv2.MORPH_CLOSE,kernelClose)
maskFinal=maskClose
_,conts,h=cv2.findContours(maskFinal.copy(),cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_NONE)
cv2.drawContours(img,conts,-1,(255,0,0),3)
for i in range(len(conts)):
x,y,w,h=cv2.boundingRect(conts[i])
cv2.rectangle(img,(x,y),(x+w,y+h),(0,0,255), 2)
contdim = [x,y,w,h]
obstacles[ocount]=contdim
ocount += 1
if conf.debug:
print("ObjectDetection:{}".format(obstacles))
return obstacles