18
18
import colorsys
19
19
import numpy as np
20
20
21
- CAMERA_REFRESH_INTERVAL = 0.1
21
+ CAMERA_REFRESH_INTERVAL = 0.01
22
22
MAX_IMAGE_AGE = 0.0
23
23
PHOTO_PATH = "./photos"
24
24
DEFAULT_IMAGE = "./photos/broken.jpg"
@@ -494,15 +494,21 @@ def find_color(self, s_color):
494
494
upper_color = np .array ([h + 10 , 255 , 255 ])
495
495
logging .debug ("lower: " + str (lower_color ) + " upper: " + str (upper_color ))
496
496
bw = cv2 .inRange (img_hsv , lower_color , upper_color )
497
- #contours, hierarchy = bw.find_contours()
498
- contours , hierarchy = cv2 .findContours (bw , cv2 .cv .CV_RETR_TREE , cv2 .cv .CV_CHAIN_APPROX_SIMPLE )
499
- for contour in contours :
500
- (x ,y ),radius = cv2 .minEnclosingCircle (contour )
497
+ # get unordered list of contours in filtered image
498
+ contours , _ = cv2 .findContours (bw , cv2 .cv .CV_RETR_LIST , cv2 .cv .CV_CHAIN_APPROX_SIMPLE )
499
+ center = (0 ,0 )
500
+ radius = 0
501
+ if not contours is None and len (contours ) > 0 :
502
+ # get the contour with the largest area
503
+ largest = sorted (contours , key = cv2 .contourArea , reverse = True )[0 ]
504
+ (x ,y ),radius = cv2 .minEnclosingCircle (largest )
501
505
center = (int (x ), int (y ))
502
506
radius = int (radius )
507
+ # draw a circle around the largest
503
508
cv2 .circle (img , center , radius , (0 ,255 ,0 ), 2 )
504
- self ._image_cache = img
505
- self ._image_cache_updated = True
509
+ # copy to image cache for jpegging and streaming [see run()]
510
+ self ._image_cache = img
511
+ self ._image_cache_updated = True
506
512
#self.save_image(bw.to_jpeg())
507
513
#objects = bw.find_blobs(minsize=5, maxsize=100)
508
514
#logging.debug("objects: " + str(objects))
@@ -523,7 +529,7 @@ def find_color(self, s_color):
523
529
#self.save_image(img.to_jpeg())
524
530
#print "object: " + str(time.time() - ts)
525
531
#return [dist, angle]
526
- return [0 , 0 ]
532
+ return [center [ 0 ], center [ 1 ], radius * 2 ]
527
533
528
534
529
535
def sleep (self , elapse ):
0 commit comments