-
Notifications
You must be signed in to change notification settings - Fork 0
/
IRVideoDetect.py
64 lines (56 loc) · 2.03 KB
/
IRVideoDetect.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
import rospy
import rospkg
from sensor_msgs.msg import Image
from std_msgs.msg import String
import numpy as np
import cv2
import time
class IRVideoDetect:
#Initialize Ros topics
def __init__(self,cap):
rospy.init_node('IR_Video_detect',anonymous=True)
self.pub = rospy.Publisher('/IR_video_detect',String,queue_size=100)
#rospy.Subscriber('/usb_camera',Image,self.video_callback)
self.cap = cap
#Callback for video cam
def video_callback(self,data):
pass
#Label the biggest red object in the video as the obstacle
# and sense if they are approaching
def color_detect(self):
while True:
ret,frame = self.cap.read()
imhsv = cv2.cvtColor(frame,cv2.COLOR_BGR2HSV)
lower_range = np.array([125, 80, 80], dtype=np.uint8)
upper_range = np.array([255, 255, 255], dtype=np.uint8)
mask = cv2.inRange(imhsv,lower_range,upper_range)
ret,thresh = cv2.threshold(mask,127,155,1)
contours,h = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
max_area = 0
for cnt in contours:
approx = cv2.approxPolyDP(cnt,0.01*cv2.arcLength(cnt,True),True)
area = cv2.contourArea(approx)
if area > max_area and area < 250000:
max_area = area
max_contour = cnt
cv2.drawContours(frame,[max_contour],0,(0,255,0),2)
if cv2.contourArea(max_contour)>15000:
self.pub.publish('Find an Obstale')
cv2.imshow('mask',mask)
cv2.imshow('frame',frame)
if cv2.waitKey(1) & 0XFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
#Running the nodes
def run(self):
r = rospy.Rate(2)
time.sleep(2)
while not rospy.is_shutdown():
self.color_detect()
r.sleep
#Testing nodes
if __name__ == '__main__':
cap = cv2.VideoCapture(0)
od = IRVideoDetect(cap)
od.run()