-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshaperecog.py
More file actions
100 lines (79 loc) · 2.84 KB
/
Copy pathshaperecog.py
File metadata and controls
100 lines (79 loc) · 2.84 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
# USAGE
# python detect_color.py --image example_shapes.png
# import the necessary packages
from pyimagesearch.shapedetector import ShapeDetector
from pyimagesearch.colorlabeler import ColorLabeler
import argparse
import imutils
import cv2
import sys
import os
os.chdir(sys.path[0])
# # construct the argument parse and parse the arguments
# ap = argparse.ArgumentParser()
# ap.add_argument("-i", "--image", required=True,
# help="path to the input image")
# args = vars(ap.parse_args())
cap = cv2.VideoCapture(0)
while True:
ret, frame = cap.read()
# load the image and resize it to a smaller factor so that
# the shapes can be approximated better
#image = cv2.imread(frame)
# height, width = frame.shape[:2]
# # Define ROI Box Dimensions (Note some of these things should be outside the loop)
# top_left_x = int(width/3)
# top_left_y = int((height / 2) + (height / 4))
# bottom_right_x = int((width / 3) * 2)
# bottom_right_y = int((height / 2) - (height / 4))
# # Draw rectangular window for our region of interest
# cv2.rectangle(frame, (top_left_x,top_left_y), (bottom_right_x,bottom_right_y), 255, 3)
# # Crop window of observation we defined above
# cropped = frame[bottom_right_y:top_left_y , top_left_x:bottom_right_x]
image = frame
resized = imutils.resize(image, width=300)
ratio = image.shape[0] / float(resized.shape[0])
# blur the resized image slightly, then convert it to both
# grayscale and the L*a*b* color spaces
blurred = cv2.GaussianBlur(resized, (5, 5), 0)
gray = cv2.cvtColor(blurred, cv2.COLOR_BGR2GRAY)
lab = cv2.cvtColor(blurred, cv2.COLOR_BGR2LAB)
thresh = cv2.threshold(gray, 60, 255, cv2.THRESH_BINARY)[1]
#cv2.imshow("Thresh", thresh)
# find contours in the thresholded image
cnts = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL,
cv2.CHAIN_APPROX_SIMPLE)
cnts = imutils.grab_contours(cnts)
# initialize the shape detector and color labeler
sd = ShapeDetector()
cl = ColorLabeler()
# loop over the contours
for c in cnts:
# compute the center of the contour
M = cv2.moments(c)
try:
cX = int((M["m10"] / M["m00"]) * ratio)
cY = int((M["m01"] / M["m00"]) * ratio)
except:
pass
# detect the shape of the contour and label the color
shape = sd.detect(c)
color = cl.label(lab, c)
# multiply the contour (x, y)-coordinates by the resize ratio,
# then draw the contours and the name of the shape and labeled
# color on the image
c = c.astype("float")
c *= ratio
c = c.astype("int")
text = "{} {}".format(color, shape)
cv2.drawContours(image, [c], -1, (0, 255, 0), 2)
cv2.putText(image, text, (cX, cY),
cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255), 2)
# # show the output image
# cv2.imshow("Image", image)
# cv2.waitKey(0)
cv2.imshow('Object Detector using ORB', frame)
if cv2.waitKey(1) == 13: #13 is the Enter Key
break
cap.release()
cv2.destroyAllWindows()