Initial commit
This commit is contained in:
8
Divers/tutoriel25/README.md
Normal file
8
Divers/tutoriel25/README.md
Normal file
@@ -0,0 +1,8 @@
|
||||
# Tutoriel 25
|
||||
## Lecture des panneaux de limitation de vitesse
|
||||
|
||||
Les vidéos de ce tutoriel sont disponibles aux adresses suivantes:<br>
|
||||
partie 1: https://www.youtube.com/watch?v=PvD5POjXw8Q <br>
|
||||
partie 2: https://www.youtube.com/watch?v=TYDi0SNCUr0 <br>
|
||||
partie 3: https://www.youtube.com/watch?v=fBysd-Y17Tw
|
||||
|
||||
30
Divers/tutoriel25/common.py
Normal file
30
Divers/tutoriel25/common.py
Normal file
@@ -0,0 +1,30 @@
|
||||
import tensorflow as tf
|
||||
from tensorflow.keras import layers, models
|
||||
import os
|
||||
import cv2
|
||||
|
||||
size=42
|
||||
dir_images_panneaux="images_panneaux"
|
||||
dir_images_autres_panneaux="images_autres_panneaux"
|
||||
dir_images_sans_panneaux="images_sans_panneaux"
|
||||
|
||||
def lire_images_panneaux(dir_images_panneaux, size=None):
|
||||
tab_panneau=[]
|
||||
tab_image_panneau=[]
|
||||
|
||||
if not os.path.exists(dir_images_panneaux):
|
||||
quit("Le repertoire d'image n'existe pas: {}".format(dir_images_panneaux))
|
||||
|
||||
files=os.listdir(dir_images_panneaux)
|
||||
if files is None:
|
||||
quit("Le repertoire d'image est vide: {}".format(dir_images_panneaux))
|
||||
|
||||
for file in sorted(files):
|
||||
if file.endswith("png"):
|
||||
tab_panneau.append(file.split(".")[0])
|
||||
image=cv2.imread(dir_images_panneaux+"/"+file)
|
||||
if size is not None:
|
||||
image=cv2.resize(image, (size, size), cv2.INTER_LANCZOS4)
|
||||
tab_image_panneau.append(image)
|
||||
|
||||
return tab_panneau, tab_image_panneau
|
||||
82
Divers/tutoriel25/dataset.py
Normal file
82
Divers/tutoriel25/dataset.py
Normal file
@@ -0,0 +1,82 @@
|
||||
import numpy as np
|
||||
import cv2
|
||||
from multiprocessing import Pool
|
||||
import multiprocessing
|
||||
import random
|
||||
|
||||
def bruit(image_orig):
|
||||
h, w, c=image_orig.shape
|
||||
n=np.random.randn(h, w, c)*random.randint(5, 30)
|
||||
return np.clip(image_orig+n, 0, 255).astype(np.uint8)
|
||||
|
||||
def change_gamma(image, alpha=1.0, beta=0.0):
|
||||
return np.clip(alpha*image+beta, 0, 255).astype(np.uint8)
|
||||
|
||||
def modif_img(img):
|
||||
h, w, c=img.shape
|
||||
|
||||
r_color=[np.random.randint(255), np.random.randint(255), np.random.randint(255)]
|
||||
img=np.where(img==[142, 142, 142], r_color, img).astype(np.uint8)
|
||||
|
||||
if np.random.randint(3):
|
||||
k_max=3
|
||||
kernel_blur=np.random.randint(k_max)*2+1
|
||||
img=cv2.GaussianBlur(img, (kernel_blur, kernel_blur), 0)
|
||||
|
||||
M=cv2.getRotationMatrix2D((int(w/2), int(h/2)), random.randint(-10, 10), 1)
|
||||
img=cv2.warpAffine(img, M, (w, h))
|
||||
|
||||
if np.random.randint(2):
|
||||
a=int(max(w, h)/5)+1
|
||||
pts1=np.float32([[0, 0], [w, 0], [0, h], [w, h]])
|
||||
pts2=np.float32([[0+random.randint(-a, a), 0+random.randint(-a, a)], [w-random.randint(-a, a), 0+random.randint(-a, a)], [0+random.randint(-a, a), h-random.randint(-a, a)], [w-random.randint(-a, a), h-random.randint(-a, a)]])
|
||||
M=cv2.getPerspectiveTransform(pts1,pts2)
|
||||
img=cv2.warpPerspective(img, M, (w, h))
|
||||
|
||||
if np.random.randint(2):
|
||||
r=random.randint(0, 5)
|
||||
h2=int(h*0.9)
|
||||
w2=int(w*0.9)
|
||||
if r==0:
|
||||
img=img[0:w2, 0:h2]
|
||||
elif r==1:
|
||||
img=img[w-w2:w, 0:h2]
|
||||
elif r==2:
|
||||
img=img[0:w2, h-h2:h]
|
||||
elif r==3:
|
||||
img=img[w-w2:w, h-h2:h]
|
||||
img=cv2.resize(img, (h, w))
|
||||
|
||||
if np.random.randint(2):
|
||||
r=random.randint(1, int(max(w, h)*0.15))
|
||||
img=img[r:w-r, r:h-r]
|
||||
img=cv2.resize(img, (h, w))
|
||||
|
||||
if not np.random.randint(4):
|
||||
t=np.empty((h, w, c) , dtype=np.float32)
|
||||
for i in range(h):
|
||||
for j in range(w):
|
||||
for k in range(c):
|
||||
t[i][j][k]=(i/h)
|
||||
M=cv2.getRotationMatrix2D((int(w/2), int(h/2)), np.random.randint(4)*90, 1)
|
||||
t=cv2.warpAffine(t, M, (w, h))
|
||||
img=(cv2.multiply((img/255).astype(np.float32), t)*255).astype(np.uint8)
|
||||
|
||||
img=change_gamma(img, random.uniform(0.6, 1.0), -np.random.randint(50))
|
||||
|
||||
if not np.random.randint(4):
|
||||
p=(15+np.random.randint(10))/100
|
||||
img=(img*p+50*(1-p)).astype(np.uint8)+np.random.randint(100)
|
||||
|
||||
img=bruit(img)
|
||||
|
||||
return img
|
||||
|
||||
def create_lot_img(image, nbr, nbr_thread=None):
|
||||
if nbr_thread is None:
|
||||
nbr_thread=multiprocessing.cpu_count()
|
||||
lot_original=np.repeat([image], nbr, axis=0)
|
||||
with Pool(nbr_thread) as p:
|
||||
lot_result=p.map(modif_img, lot_original)
|
||||
p.close()
|
||||
return lot_result
|
||||
46
Divers/tutoriel25/extract_panneau.py
Normal file
46
Divers/tutoriel25/extract_panneau.py
Normal file
@@ -0,0 +1,46 @@
|
||||
import cv2
|
||||
import os
|
||||
import numpy as np
|
||||
import random
|
||||
import common
|
||||
|
||||
video_dir="D:\dashcam Cedric"
|
||||
|
||||
l=os.listdir(video_dir)
|
||||
|
||||
for video in l:
|
||||
if not video.endswith("mp4"):
|
||||
continue
|
||||
cap=cv2.VideoCapture(video_dir+"/"+video)
|
||||
|
||||
print("video:", video)
|
||||
while True:
|
||||
ret, frame=cap.read()
|
||||
if ret is False:
|
||||
break
|
||||
f_w, f_h, f_c=frame.shape
|
||||
frame=cv2.resize(frame, (int(f_h/1.5), int(f_w/1.5)))
|
||||
|
||||
image=frame[200:400, 700:1000]
|
||||
cv2.rectangle(frame, (700, 200), (1000, 400), (255, 255, 255), 1)
|
||||
|
||||
gray=cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
|
||||
circles=cv2.HoughCircles(gray, cv2.HOUGH_GRADIENT, 1, 20, param1=30, param2=60, minRadius=5, maxRadius=45)
|
||||
if circles is not None:
|
||||
circles=np.int16(np.around(circles))
|
||||
for i in circles[0,:]:
|
||||
if i[2]!=0:
|
||||
panneau=cv2.resize(image[max(0, i[1]-i[2]):i[1]+i[2], max(0, i[0]-i[2]):i[0]+i[2]], (common.size, common.size))/255
|
||||
cv2.imshow("panneau", panneau)
|
||||
cv2.putText(frame, "fichier:"+video, (30, 30), cv2.FONT_HERSHEY_DUPLEX, 1, (0, 255, 0), 1, cv2.LINE_AA)
|
||||
cv2.imshow("Video", frame)
|
||||
key=cv2.waitKey(1)&0xFF
|
||||
if key==ord('q'):
|
||||
quit()
|
||||
if key==ord('a'):
|
||||
for cpt in range(100):
|
||||
ret, frame=cap.read()
|
||||
if key==ord('f'):
|
||||
break
|
||||
|
||||
cv2.destroyAllWindows()
|
||||
29
Divers/tutoriel25/genere_panneaux.py
Normal file
29
Divers/tutoriel25/genere_panneaux.py
Normal file
@@ -0,0 +1,29 @@
|
||||
import numpy as np
|
||||
from sklearn.utils import shuffle
|
||||
import cv2
|
||||
import common
|
||||
import dataset
|
||||
|
||||
tab_panneau, tab_image_panneau=common.lire_images_panneaux(common.dir_images_panneaux, common.size)
|
||||
|
||||
tab_images=np.array([]).reshape(0, common.size, common.size, 3)
|
||||
tab_labels=[]
|
||||
|
||||
id=0
|
||||
for image in tab_image_panneau:
|
||||
lot=dataset.create_lot_img(image, 1000)
|
||||
tab_images=np.concatenate([tab_images, lot])
|
||||
tab_labels=np.concatenate([tab_labels, np.full(len(lot), id)])
|
||||
id+=1
|
||||
|
||||
tab_panneau=np.array(tab_panneau)
|
||||
tab_images=np.array(tab_images, dtype=np.float32)/255
|
||||
tab_labels=np.array(tab_labels).reshape([-1, 1])
|
||||
|
||||
tab_images, tab_labels=shuffle(tab_images, tab_labels)
|
||||
|
||||
for i in range(len(tab_images)):
|
||||
cv2.imshow("panneau", tab_images[i])
|
||||
print("label", tab_labels[i], "panneau", tab_panneau[int(tab_labels[i])])
|
||||
if cv2.waitKey()&0xFF==ord('q'):
|
||||
quit()
|
||||
36
Divers/tutoriel25/houghcircles.py
Normal file
36
Divers/tutoriel25/houghcircles.py
Normal file
@@ -0,0 +1,36 @@
|
||||
import cv2
|
||||
import numpy as np
|
||||
|
||||
param1=30
|
||||
param2=55
|
||||
dp=1.0
|
||||
|
||||
cap=cv2.VideoCapture(0)
|
||||
|
||||
while True:
|
||||
ret, frame=cap.read()
|
||||
gray=cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
|
||||
circles=cv2.HoughCircles(gray, cv2.HOUGH_GRADIENT, dp, 20, param1=param1, param2=param2, minRadius=10, maxRadius=50)
|
||||
if circles is not None:
|
||||
circles=np.around(circles).astype(np.int32)
|
||||
for i in circles[0, :]:
|
||||
if i[2]!=0:
|
||||
cv2.circle(frame, (i[0], i[1]), i[2], (0, 255, 0), 4)
|
||||
cv2.putText(frame, "[i|k]dp: {:4.2f} [o|l]param1: {:d} [p|m]param2: {:d}".format(dp, param1, param2), (10, 40), cv2.FONT_HERSHEY_COMPLEX_SMALL, 1, (255, 0, 0), 1)
|
||||
cv2.imshow("Video", frame)
|
||||
key=cv2.waitKey(1)&0xFF
|
||||
if key==ord('q'):
|
||||
quit()
|
||||
if key==ord('i'):
|
||||
dp=min(10, dp+0.1)
|
||||
if key==ord('k'):
|
||||
dp=max(0.1, dp-0.1)
|
||||
if key==ord('o'):
|
||||
param1=min(255, param1+1)
|
||||
if key==ord('l'):
|
||||
param1=max(1, param1-1)
|
||||
if key==ord('p'):
|
||||
param2=min(255, param2+1)
|
||||
if key==ord('m'):
|
||||
param2=max(1, param2-1)
|
||||
cv2.destroyAllWindows()
|
||||
Reference in New Issue
Block a user