-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDétection_d'objet.py
33 lines (30 loc) · 1.29 KB
/
Détection_d'objet.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
import cv2
import numpy as np
lo = np.array([20, 100, 100]) # Valeurs MINIMALES HSV pour le jaune
hi = np.array([30, 255, 255]) # Valeurs MAXIMALES HSV pour le jaune
color_infos = (0, 255, 255)
cap=cv2.VideoCapture(0)
while True:
ret, frame=cap.read()
image=cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
mask=cv2.inRange(image, lo, hi)
image=cv2.blur(image, (7, 7))
mask=cv2.erode(mask, None, iterations=4)
mask=cv2.dilate(mask, None, iterations=4)
image2=cv2.bitwise_and(frame, frame, mask=mask)
elements=cv2.findContours(mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)[-2]
if len(elements) > 0:
c=max(elements, key=cv2.contourArea)
((x, y), rayon)=cv2.minEnclosingCircle(c)
if rayon>30:
cv2.circle(image2, (int(x), int(y)), int(rayon), color_infos, 2)
cv2.circle(frame, (int(x), int(y)), 5, color_infos, 10)
cv2.line(frame, (int(x), int(y)), (int(x)+150, int(y)), color_infos, 2)
cv2.putText(frame, "Objet !!!", (int(x)+10, int(y) -10), cv2.FONT_HERSHEY_DUPLEX, 1, color_infos, 1, cv2.LINE_AA)
cv2.imshow('Camera', frame)
cv2.imshow('image2', image2)
cv2.imshow('Mask', mask)
if cv2.waitKey(1)&0xFF==ord('q'):
break
cap.release()
cv2.destroyAllWindows()