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
| color = np.uint8([[[203, 192, 255]]]) hsv = cv2.cvtColor(color, cv2.COLOR_BGR2HSV) ```
```Python import numpy as np import cv2
font = cv2.FONT_HERSHEY_SIMPLEX lower_green = np.array([35, 110, 106]) upper_green = np.array([77, 255, 255])
cap = cv2.VideoCapture('F:/test/mp4.mp4') if cap.isOpened(): flag = 1 else: flag = 0 num = 0 if flag: while True: ret, frame = cap.read()
if not ret: break hsv_img = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV) mask_green = cv2.inRange(hsv_img, lower_green, upper_green)
mask_green = cv2.medianBlur(mask_green, 7) mask_green, contours, hierarchy = cv2.findContours(mask_green, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
for cnt in contours: (x, y, w, h) = cv2.boundingRect(cnt) cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 255), 2) cv2.putText(frame, "Green", (x, y - 5), font, 0.7, (0, 255, 0), 2)
num = num + 1 cv2.imshow("dection", frame) cv2.imwrite("imgs/%d.jpg" % num, frame) if cv2.waitKey(20) & 0xFF == 27: break cv2.waitKey(0) cv2.destroyAllWindows()
|