-
Notifications
You must be signed in to change notification settings - Fork 0
/
tracking.py
58 lines (55 loc) · 2.07 KB
/
tracking.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
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
from imutils.video.pivideostream import PiVideoStream
from imutils.video import FPS # Pour mesurer les FPS
import cv2 # Librairie OpenCV
import time # Fonctions temporelles
# Definition des seuils HSV pour le vert
greenLower = (85, 126, 60)
greenUpper = (95, 255, 255)
# Creation de la fenetre d'affichage
cv2.namedWindow('Tracking', cv2.WINDOW_NORMAL)
# Demarage du flux video sur un thread different
vs = PiVideoStream()
vs.camera.video_stabilization = True
vs.start()
# Laisse le temps de chauffer a la camera
time.sleep(1.0)
# Demarre le compteur de FPS
fps = FPS().start()
# Boucle principale
while True :
frame = vs.read()
#frame = imutils.resize(frame, width = 400)
# Convertis la frame en HSV
hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
# Construis un masque pour la couleur définie
mask = cv2.inRange(hsv, greenLower, greenUpper)
# Effectue 2 passages d'erosion pour retirer les petits éléments
mask = cv2.erode(mask, None, iterations = 2)
# Effectue 2 passages de dilatation pour conserver la taille
mask = cv2.dilate(mask, None, iterations = 2)
# Detecte les contours dans le masque
cnts = cv2.findContours(mask.copy(), cv2.RETR_EXTERNAL,
cv2.CHAIN_APPROX_SIMPLE)[-2]
# Si on a trouvé un contours
if len(cnts) > 0 :
# Dans c on met le plus grand contours trouvé
c = max(cnts, key = cv2.contourArea)
# On utlise c pour calculer le cercle qui l'entours
((x,y), radius) = cv2.minEnclosingCircle(c)
# Si le cercle a un rayon minimum
if radius > 6 :
# Dessine le cercle sur la frame
cv2.circle(frame, (int(x), int(y)), 6, (0,255, 255), -1)
# Mets a jour le compteur de FPS
fps.update()
# Affiche l'image a l'ecran
cv2.imshow('Tracking', frame)
# Si on appuie sur q, arrete la boucle
if cv2.waitKey(1) & 0xFF == ord("q") :
break
fps.stop()
print("[INFO] elasped time: {:.2f}".format(fps.elapsed()))
print("[INFO] approx. FPS: {:.2f}".format(fps.fps()))
# Ferme les fenetres ouvertes
cv2.destroyAllWindows()
vs.stop()