Initial commit
This commit is contained in:
33
Divers/tutoriel36/Detector.py
Normal file
33
Divers/tutoriel36/Detector.py
Normal file
@@ -0,0 +1,33 @@
|
||||
import numpy as np
|
||||
import cv2
|
||||
|
||||
lo=np.array([80, 50, 50])
|
||||
hi=np.array([100, 255, 255])
|
||||
|
||||
def detect_inrange(image, surface):
|
||||
points=[]
|
||||
image = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
|
||||
image=cv2.blur(image, (5, 5))
|
||||
mask=cv2.inRange(image, lo, hi)
|
||||
mask=cv2.erode(mask, None, iterations=2)
|
||||
mask=cv2.dilate(mask, None, iterations=2)
|
||||
elements=cv2.findContours(mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)[-2]
|
||||
elements=sorted(elements, key=lambda x:cv2.contourArea(x), reverse=True)
|
||||
for element in elements:
|
||||
if cv2.contourArea(element)>surface:
|
||||
((x, y), rayon)=cv2.minEnclosingCircle(element)
|
||||
points.append(np.array([int(x), int(y)]))
|
||||
else:
|
||||
break
|
||||
|
||||
return points, mask
|
||||
|
||||
def detect_visage(image):
|
||||
face_cascade=cv2.CascadeClassifier("./haarcascade_frontalface_alt2.xml")
|
||||
points=[]
|
||||
gray=cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
|
||||
face=face_cascade.detectMultiScale(gray, scaleFactor=1.2, minNeighbors=3)
|
||||
for x, y, w, h in face:
|
||||
points.append(np.array([int(x+w/2), int(y+h/2)]))
|
||||
|
||||
return points, None
|
||||
46
Divers/tutoriel36/KalmanFilter.py
Normal file
46
Divers/tutoriel36/KalmanFilter.py
Normal file
@@ -0,0 +1,46 @@
|
||||
import numpy as np
|
||||
|
||||
class KalmanFilter(object):
|
||||
def __init__(self, dt, point):
|
||||
self.dt=dt
|
||||
|
||||
# Vecteur d'etat initial
|
||||
self.E=np.matrix([[point[0]], [point[1]], [0], [0]])
|
||||
|
||||
# Matrice de transition
|
||||
self.A=np.matrix([[1, 0, self.dt, 0],
|
||||
[0, 1, 0, self.dt],
|
||||
[0, 0, 1, 0],
|
||||
[0, 0, 0, 1]])
|
||||
|
||||
# Matrice d'observation, on observe que x et y
|
||||
self.H=np.matrix([[1, 0, 0, 0],
|
||||
[0, 1, 0, 0]])
|
||||
|
||||
self.Q=np.matrix([[1, 0, 0, 0],
|
||||
[0, 1, 0, 0],
|
||||
[0, 0, 1, 0],
|
||||
[0, 0, 0, 1]])
|
||||
|
||||
self.R=np.matrix([[1, 0],
|
||||
[0, 1]])
|
||||
|
||||
self.P=np.eye(self.A.shape[1])
|
||||
|
||||
def predict(self):
|
||||
self.E=np.dot(self.A, self.E)
|
||||
# Calcul de la covariance de l'erreur
|
||||
self.P=np.dot(np.dot(self.A, self.P), self.A.T)+self.Q
|
||||
return self.E
|
||||
|
||||
def update(self, z):
|
||||
# Calcul du gain de Kalman
|
||||
S=np.dot(self.H, np.dot(self.P, self.H.T))+self.R
|
||||
K=np.dot(np.dot(self.P, self.H.T), np.linalg.inv(S))
|
||||
|
||||
# Correction / innovation
|
||||
self.E=np.round(self.E+np.dot(K, (z-np.dot(self.H, self.E))))
|
||||
I=np.eye(self.H.shape[1])
|
||||
self.P=(I-(K*self.H))*self.P
|
||||
|
||||
return self.E
|
||||
5
Divers/tutoriel36/README.md
Normal file
5
Divers/tutoriel36/README.md
Normal file
@@ -0,0 +1,5 @@
|
||||
# Tutoriel 36
|
||||
## Filtre de Kalman
|
||||
|
||||
La vidéo de ce tutoriel est disponible à l'adresse suivante: https://www.youtube.com/watch?v=IT4i_ooQDDM
|
||||
|
||||
20719
Divers/tutoriel36/haarcascade_frontalface_alt2.xml
Normal file
20719
Divers/tutoriel36/haarcascade_frontalface_alt2.xml
Normal file
File diff suppressed because it is too large
Load Diff
29690
Divers/tutoriel36/haarcascade_profileface.xml
Normal file
29690
Divers/tutoriel36/haarcascade_profileface.xml
Normal file
File diff suppressed because it is too large
Load Diff
65
Divers/tutoriel36/selectionne_couleur.py
Normal file
65
Divers/tutoriel36/selectionne_couleur.py
Normal file
@@ -0,0 +1,65 @@
|
||||
import cv2
|
||||
import numpy as np
|
||||
|
||||
def souris(event, x, y, flags, param):
|
||||
global lo, hi, color
|
||||
if event==cv2.EVENT_LBUTTONDBLCLK:
|
||||
color=image[y, x][0]
|
||||
if event==cv2.EVENT_MOUSEWHEEL:
|
||||
if flags<0:
|
||||
if color>5:
|
||||
color-=1
|
||||
else:
|
||||
if color<250:
|
||||
color+=1
|
||||
lo[0]=color-15
|
||||
hi[0]=color+15
|
||||
|
||||
color=90
|
||||
S=50
|
||||
V=50
|
||||
lo=np.array([color-5, S, V])
|
||||
hi=np.array([color+5, 255,255])
|
||||
color_info=(0, 255, 0)
|
||||
cap=cv2.VideoCapture(0)
|
||||
cv2.namedWindow('Camera')
|
||||
cv2.setMouseCallback('Camera', souris)
|
||||
while True:
|
||||
ret, frame=cap.read()
|
||||
image=cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
|
||||
image=cv2.blur(image, (5, 5))
|
||||
mask=cv2.inRange(image, lo, hi)
|
||||
mask=cv2.erode(mask, None, iterations=2)
|
||||
mask=cv2.dilate(mask, None, iterations=8)
|
||||
elements=cv2.findContours(mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)[-2]
|
||||
if len(elements) > 0:
|
||||
c=max(elements, key=cv2.contourArea)
|
||||
((x, y), radius)=cv2.minEnclosingCircle(c)
|
||||
if radius>10:
|
||||
cv2.circle(frame, (int(x), int(y)), 5, color_info, 10)
|
||||
cv2.line(frame, (int(x), int(y)), (int(x)+150, int(y)), color_info, 2)
|
||||
cv2.putText(frame, "Objet !!!", (int(x)+10, int(y) -10), cv2.FONT_HERSHEY_DUPLEX, 1, color_info, 1, cv2.LINE_AA)
|
||||
|
||||
cv2.rectangle(frame, (0, 0), (frame.shape[1], 30), (100, 100, 100), cv2.FILLED)
|
||||
cv2.putText(frame, "[Souris]Couleur: {:d} [o|l] S:{:d} [p|m] V{:d}".format(color, S, V), (5, 20), cv2.FONT_HERSHEY_PLAIN, 1, (255, 255, 255), 1, cv2.LINE_AA)
|
||||
cv2.imshow('Camera', frame)
|
||||
cv2.imshow('Mask', mask)
|
||||
|
||||
key=cv2.waitKey(1)&0xFF
|
||||
if key==ord('q'):
|
||||
break
|
||||
if key==ord('p'):
|
||||
V=min(255, V+1)
|
||||
lo=np.array([color-5, S, V])
|
||||
if key==ord('m'):
|
||||
V=max(1, V-1)
|
||||
lo=np.array([color-5, S, V])
|
||||
if key==ord('o'):
|
||||
S=min(255, S+1)
|
||||
lo=np.array([color-5, S, V])
|
||||
if key==ord('l'):
|
||||
S=max(1, S-1)
|
||||
lo=np.array([color-5, S, V])
|
||||
|
||||
cap.release()
|
||||
cv2.destroyAllWindows()
|
||||
35
Divers/tutoriel36/suivi.py
Normal file
35
Divers/tutoriel36/suivi.py
Normal file
@@ -0,0 +1,35 @@
|
||||
import cv2
|
||||
from Detector import detect_inrange, detect_visage
|
||||
from KalmanFilter import KalmanFilter
|
||||
import numpy as np
|
||||
|
||||
VideoCap=cv2.VideoCapture(0)
|
||||
|
||||
KF=KalmanFilter(0.1, [0, 0])
|
||||
|
||||
while(True):
|
||||
ret, frame=VideoCap.read()
|
||||
|
||||
points, mask=detect_inrange(frame, 800)
|
||||
#points, mask=detect_visage(frame)
|
||||
|
||||
etat=KF.predict().astype(np.int32)
|
||||
|
||||
cv2.circle(frame, (int(etat[0]), int(etat[1])), 2, (0, 255, 0), 5)
|
||||
cv2.arrowedLine(frame,
|
||||
(etat[0], etat[1]), (etat[0]+etat[2], etat[1]+etat[3]),
|
||||
color=(0, 255, 0),
|
||||
thickness=3,
|
||||
tipLength=0.2)
|
||||
if (len(points)>0):
|
||||
cv2.circle(frame, (points[0][0], points[0][1]), 10, (0, 0, 255), 2)
|
||||
KF.update(np.expand_dims(points[0], axis=-1))
|
||||
|
||||
cv2.imshow('image', frame)
|
||||
if mask is not None:
|
||||
cv2.imshow('mask', mask)
|
||||
|
||||
if cv2.waitKey(1)&0xFF==ord('q'):
|
||||
VideoCap.release()
|
||||
cv2.destroyAllWindows()
|
||||
break
|
||||
Reference in New Issue
Block a user