Le code modifié pour l’utilisation de la webcam => Sur PC avec une bonne carte graphique => 8Fps + lag d’une bonne seconde.
[pastacode lang= »python » manual= »%23%20From%20Exemple%2013%20%2F%20Compte%20les%20v%C3%A9hicules.%0Aimport%20cv2%0Aimport%20numpy%20as%20np%0Aimport%20time%0A%0Acap%20%3D%20cv2.VideoCapture(0)%0A%0AC1%3D0%0Acolor_infos%3D(0%2C%200%2C%20255)%0A%23Dimension%20du%20Mask%20(%2Bgrand%20%3D%20moins%20de%20FPS)%0A%23%20y%20%3D%20Hauteur%20(rappel%20r%C3%A9solution%20480)%0Aymin%3D300%23315%0Aymax%3D450%23360%0Ay_dim%20%3D%20ymax-ymin%0A%23%20x%3Dlargeur%20(rappel%20r%C3%A9solution%20640)%0Axmin%3D10%0Axmax%20%3D%20600%0Ax_dim%20%3D%20xmax-xmin%0A%0Aseuil%3D10%0Aseuil2%3D3%20%23%20ajuster%20%C3%A0%20la%20taille%20de%20ces%20fucking%20frelons%0Aseuil3%3Dseuil2%20%23%20seuil%20trop%20de%20diff%C3%A9rence%20%3D%3E%20mvt%20camera%20%3D%3E%20rachraichir%20fond%0A%0A%23initialisation%20matrice%20Tab_Image%20et%20Fond%0Atab_image%3Dnp.zeros(%5By_dim%2C%20x_dim%5D%2C%20np.uint8)%0Afond%3Dnp.zeros(%5By_dim%2C%20x_dim%5D%2C%20np.uint8)%0A%0A%23print(%22fond.shape1%22%2Cfond.shape)%0Atab_image%20%3D%20tab_image%20%2B%20fond%0A%23print(%22tab_image.shape1%22%2Ctab_image.shape)%0A%0Adef%20calcul_mean(image)%3A%0A%20%20%20%20height%2C%20width%3Dimage.shape%0A%20%20%20%20s%3D0%0A%20%20%20%20for%20y%20in%20range(height)%3A%0A%20%20%20%20%20%20%20%20for%20x%20in%20range(width)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20s%2B%3Dimage%5By%5D%5Bx%5D%0A%20%20%20%20return%20s%2F(height*width)%0A%0Adef%20calcul_mask(image%2C%20fond%2C%20seuil)%3A%0A%20%20%20%20image%3Dcv2.cvtColor(image%2C%20cv2.COLOR_BGR2GRAY)%0A%20%20%20%20height%2C%20width%3Dimage.shape%0A%20%20%20%20height_i%2C%20width_i%3Dfond.shape%0A%20%20%20%20if%20height%20!%3D%20height_i%3A%0A%20%20%20%20%20%20%20%20print(%22dif_heigt%22%2Cheight-height_i)%0A%20%20%20%20if%20width%20!%3D%20width_i%3A%0A%20%20%20%20%20%20%20%20print(%22dif_width%22%2Cwidth%20-%20width_i)%0A%0A%20%20%20%20mask%3Dnp.zeros(%5Bheight%2C%20width%5D%2C%20np.uint8)%0A%20%20%20%20image%3Dimage.astype(np.int32)%0A%20%20%20%20for%20y%20in%20range(height)%3A%0A%20%20%20%20%20%20%20%20for%20x%20in%20range(width)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20if%20abs(fond%5By%5D%5Bx%5D-image%5By%5D%5Bx%5D)%3Eseuil%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20mask%5By%5D%5Bx%5D%3D255%0A%20%20%20%20kernel%3Dnp.ones((5%2C%205)%2C%20np.uint8)%0A%20%20%20%20mask%3Dcv2.erode(mask%2C%20kernel%2C%20iterations%3D1)%0A%20%20%20%20mask%3Dcv2.dilate(mask%2C%20kernel%2C%20iterations%3D3)%0A%20%20%20%20return%20mask%0A%0Awhile%20True%3A%0A%20%20%20%20ret%2C%20frame%20%3D%20cap.read()%0A%20%20%20%20frame%20%3D%20cv2.cvtColor(frame%2C%20cv2.COLOR_BGRA2BGR)%20%0A%20%20%20%20tickmark%3Dcv2.getTickCount()%0A%20%20%20%20mask%3Dcalcul_mask(frame%5Bymin%3Aymax%2C%20xmin%3Axmax%5D%2C%20fond%2C%20seuil)%0A%20%20%20%20moyenne_mask%20%3D%20calcul_mean(mask%5B0%3Ay_dim%2C%200%3Ax_dim%5D)%0A%20%20%20%20if%20moyenne_mask%20%3E%20seuil3%3A%0A%20%20%20%20%20%20%20%20image%3Dcv2.cvtColor(frame%5Bymin%3Aymax%2C%20xmin%3Axmax%5D%2C%20cv2.COLOR_BGR2GRAY)%0A%20%20%20%20%20%20%20%20%23print(%22image.shape%22%2Cimage.shape)%0A%20%20%20%20%20%20%20%20%23print(%22tab_image.shape%22%2Ctab_image.shape)%0A%20%20%20%20%20%20%20%20tab_image%20%3D%20(tab_image%2Bimage)%2F2%0A%20%20%20%20%20%20%20%20fond%3Dtab_image%0A%20%20%20%20%20%20%20%20%23print(%22tab_image%22%2Ctab_image)%20%20%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20%23print(%22fond_2%22%2Cfond)%20%20%20%0A%20%20%20%20%0A%20%20%20%20if%20calcul_mean(mask%5B0%3Ay_dim%2C%200%3Ax_dim%5D)%3E%20seuil2%3A%0A%20%20%20%20%20%20%20%20message%3D%22Detection%20Moy%3A%20%22%2Bstr(moyenne_mask)%0A%20%20%20%20else%3A%0A%20%20%20%20%20%20%20%20message%3D%22RAS%20Moy%3A%20%22%2Bstr(moyenne_mask)%0A%20%20%20%20%20%20%20%20%0A%0A%20%20%20%20fps%3Dcv2.getTickFrequency()%2F(cv2.getTickCount()-tickmark)%0A%20%20%20%20cv2.putText(frame%2C%20%22FPS%3A%20%7B%3A05.2f%7D%20%20Seuil%3A%20%7B%3Ad%7D%20Tick%20%7B%3Ad%7D%22.format(fps%2C%20seuil%2C%20tickmark%2C%20message)%2C%20(1%2C%2030)%2C%20cv2.FONT_HERSHEY_COMPLEX_SMALL%2C%201%2C%20color_infos%2C%201)%0A%20%20%20%20cv2.putText(frame%2C%20%22Detect%20%3A%20%22%20%2B%20message%2C%20(1%2C%2050)%2C%20cv2.FONT_HERSHEY_COMPLEX_SMALL%2C%201%2C%20color_infos%2C%201)%0A%20%20%20%20%0A%20%20%20%20cv2.imshow(‘video’%2C%20frame)%0A%20%20%20%20cv2.imshow(‘mask’%2C%20mask)%0A%20%20%20%20cv2.imshow(‘fond’%2C%20fond.astype(np.uint8))%0A%0A%20%20%20%20key%3Dcv2.waitKey(1)%260xFF%0A%20%20%20%20if%20key%3D%3Dord(‘q’)%3A%0A%20%20%20%20%20%20%20%20break%0A%20%20%20%20if%20key%3D%3Dord(‘p’)%3A%0A%20%20%20%20%20%20%20%20seuil%2B%3D1%0A%20%20%20%20if%20key%3D%3Dord(‘m’)%3A%0A%20%20%20%20%20%20%20%20seuil-%3D1%0A%0Acap.release()%0Acv2.destroyAllWindows()%0A » message= »Detection Mouvement / Classe native CV2″ highlight= » » provider= »manual »/]