objectImageIdentifierAI/cropObjects/detectObjects.py
cropObjects/detectObjects.pyRaw
1import cv2
2#reading the image
3#image = cv2.imread("demo.jpeg")
4
5def detectObjects(image):
6 edged = cv2.Canny(image, 10, 250)
7 cv2.imshow("Edges", edged)
8 cv2.waitKey(0)
9
10 #applying closing function
11 kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (7, 7))
12 closed = cv2.morphologyEx(edged, cv2.MORPH_CLOSE, kernel)
13 cv2.imshow("Closed", closed)
14 cv2.waitKey(0)
15
16 #finding_contours
17 (_, cnts, _) = cv2.findContours(closed.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
18
19 for c in cnts:
20 peri = cv2.arcLength(c, True)
21 approx = cv2.approxPolyDP(c, 0.02 * peri, True)
22 cv2.drawContours(image, [approx], -1, (0, 255, 0), 2)
23 cv2.imshow("Output", image)
24 cv2.waitKey(0)