Initial commit

This commit is contained in:
2026-03-31 13:28:59 +02:00
commit 7ec43ca17d
314 changed files with 189852 additions and 0 deletions

View File

@@ -0,0 +1,5 @@
# Tutoriel 18 partie 2
## Sudoku
La vidéo de ce tutoriel est disponible à l'adresse suivante: https://www.youtube.com/watch?v=XFNg8lXe-Tk

Binary file not shown.

View File

@@ -0,0 +1,18 @@
from PIL import ImageFont, ImageDraw, Image
import cv2
import numpy as np
for i in range(1, 10):
image=Image.new("L", (28, 28))
draw=ImageDraw.Draw(image)
font=ImageFont.truetype("din1451altG.ttf", 27)
text="{:d}".format(i)
draw.text((10, 0), text, font=font, fill=(255))
image=np.array(image).reshape(28, 28, 1)
cv2.imshow("image", image)
key=cv2.waitKey()
if key&0xFF==ord('q'):
quit()

View File

@@ -0,0 +1,100 @@
import cv2
import numpy as np
import tensorflow as tf
import sudoku_solver as ss
from time import sleep
import operator
marge=4
case=28+2*marge
taille_grille=9*case
flag=0
cap=cv2.VideoCapture(0)
with tf.Session() as s:
saver=tf.train.import_meta_graph('./mon_modele/modele.meta')
saver.restore(s, tf.train.latest_checkpoint('./mon_modele/'))
graph=tf.get_default_graph()
images=graph.get_tensor_by_name("entree:0")
sortie=graph.get_tensor_by_name("sortie:0")
is_training=graph.get_tensor_by_name("is_training:0")
maxArea=0
while True:
ret, frame=cap.read()
if maxArea==0:
cv2.imshow("frame", frame)
gray=cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
gray=cv2.GaussianBlur(gray, (5, 5), 0)
thresh=cv2.adaptiveThreshold(gray, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY_INV, 9, 2)
contours, hierarchy=cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
contour_grille=None
maxArea=0
for c in contours:
area=cv2.contourArea(c)
if area>25000:
peri=cv2.arcLength(c, True)
polygone=cv2.approxPolyDP(c, 0.02*peri, True)
if area>maxArea and len(polygone)==4:
contour_grille=polygone
maxArea=area
if contour_grille is not None:
points=np.vstack(contour_grille).squeeze()
points=sorted(points, key=operator.itemgetter(1))
if points[0][0]<points[1][0]:
if points[3][0]<points[2][0]:
pts1=np.float32([points[0], points[1], points[3], points[2]])
else:
pts1=np.float32([points[0], points[1], points[2], points[3]])
else:
if points[3][0]<points[2][0]:
pts1=np.float32([points[1], points[0], points[3], points[2]])
else:
pts1=np.float32([points[1], points[0], points[2], points[3]])
pts2=np.float32([[0, 0], [taille_grille, 0], [0, taille_grille], [taille_grille, taille_grille]])
M=cv2.getPerspectiveTransform(pts1, pts2)
grille=cv2.warpPerspective(frame, M, (taille_grille, taille_grille))
grille=cv2.cvtColor(grille, cv2.COLOR_BGR2GRAY)
grille=cv2.adaptiveThreshold(grille, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY_INV, 9, 2)
cv2.imshow("grille", grille)
if flag==0:
grille=grille/255
grille_txt=[]
for y in range(9):
ligne=""
for x in range(9):
y2min=y*case+marge
y2max=(y+1)*case-marge
x2min=x*case+marge
x2max=(x+1)*case-marge
prediction=s.run(sortie, feed_dict={images: [grille[y2min:y2max, x2min:x2max].reshape(28, 28, 1)], is_training: False})
ligne+="{:d}".format(np.argmax(prediction[0]))
grille_txt.append(ligne)
result=ss.sudoku(grille_txt)
print("Resultat:", result)
#result=None
if result is not None:
flag=1
fond=np.zeros(shape=(taille_grille, taille_grille, 3), dtype=np.float32)
for y in range(len(result)):
for x in range(len(result[y])):
if grille_txt[y][x]=="0":
cv2.putText(fond, "{:d}".format(result[y][x]), ((x)*case+marge+3, (y+1)*case-marge-3), cv2.FONT_HERSHEY_SCRIPT_COMPLEX, 0.9, (0, 0, 255), 1)
M=cv2.getPerspectiveTransform(pts2, pts1)
h, w, c=frame.shape
fondP=cv2.warpPerspective(fond, M, (w, h))
img2gray=cv2.cvtColor(fondP, cv2.COLOR_BGR2GRAY)
ret, mask=cv2.threshold(img2gray, 10, 255, cv2.THRESH_BINARY)
mask=mask.astype('uint8')
mask_inv=cv2.bitwise_not(mask)
img1_bg=cv2.bitwise_and(frame, frame, mask=mask_inv)
img2_fg=cv2.bitwise_and(fondP, fondP, mask=mask).astype('uint8')
dst=cv2.add(img1_bg, img2_fg)
cv2.imshow("frame", dst)
else:
cv2.imshow("frame", frame)
else:
flag=0
key=cv2.waitKey(1)&0xFF
if key==ord('q'):
break
cap.release()
cv2.destroyAllWindows()

View File

@@ -0,0 +1,89 @@
def sudoku(f):
def af(g):
for n,l in enumerate(g):
for m,c in enumerate(l):
P(str(c).replace("0","."),end="")
if m in {2,5}:
P("+",end="")
P()
if n in {2,5}:
P("+"*11)
def cp(q,s):
l=set(s[q[0]])
l|={s[i][q[1]] for i in range(9)}
k=q[0]//3,q[1]//3
for i in range(3):
l|=set(s[k[0]*3+i][k[1]*3:(k[1]+1)*3])
return set(range(1,10))-l
def ec(l):
q=set(l)-{0}
for c in q:
if l.count(c)!=1:
return True
return False
# Remplissage de la grille et tests de format
P=print
af(f)
s=[]
t=[]
for nl,l in enumerate(f):
try:
n=list(map(int,l))
except:
P("La ligne "+str(nl+1)+" contient autre chose qu'un chiffre.")
return
if len(n)!=9:
P("La ligne "+str(nl+1)+" ne contient pas 9 chiffres.")
return
t+=[[nl,i] for i in range(9) if n[i]==0]
s.append(n)
if nl!=8:
P("Le jeu contient "+str(nl+1)+" lignes au lieu de 9.")
return
# Tests de validite
for l in range(9):
if ec(s[l]):
P("La ligne "+str(l+1)+" est contradictoire.")
return
for c in range(9):
k=[s[l][c] for l in range(9)]
if ec(k):
P("La colonne "+str(c+1)+" est contradictoire.")
return
for l in range(3):
for c in range(3):
q=[]
for i in range(3):
q+=s[l*3+i][c*3:(c+1)*3]
if ec(q):
P("La cellule ("+str(l+1)+";"+str(c+1)+") est contradictoire.")
return
# Resolution
p=[[] for i in t]
cr=0
while cr<len(t):
p[cr]=cp(t[cr],s)
try:
while not p[cr]:
s[t[cr][0]][t[cr][1]]=0
cr-=1
except:
P("Le sudoku n'a pas de solution.")
return
s[t[cr][0]][t[cr][1]]=p[cr].pop()
cr+=1
# Presentation de la grille resolue
af(s)
return(s)

View File

@@ -0,0 +1,147 @@
import tensorflow as tf
import numpy as np
from sklearn.utils import shuffle
from sklearn.model_selection import train_test_split
import cv2
import os
import numpy as np
from PIL import ImageFont, ImageDraw, Image
taille_batch=100
nbr_entrainement=100
nbr=2*42
def modif_image(image, seuil=1):
b=np.random.normal(0, 1, (28, 28))
a=image.copy()
a[b>seuil]=255
a[b<-seuil]=0
return a
def convolution(input, taille_noyau, nbr_noyau, stride, b_norm=False, f_activation=None, training=False):
w_filtre=tf.Variable(tf.random.truncated_normal(shape=(taille_noyau, taille_noyau, int(input.get_shape()[-1]), nbr_noyau)))
b_filtre=np.zeros(nbr_noyau)
result=tf.nn.conv2d(input, w_filtre, strides=[1, stride, stride, 1], padding='SAME')+b_filtre
if b_norm is True:
result=tf.layers.batch_normalization(result, training=training)
if f_activation is not None:
result=f_activation(result)
return result
def fc(input, nbr_neurone, b_norm=False, f_activation=None, training=False):
w=tf.Variable(tf.random.truncated_normal(shape=(int(input.get_shape()[-1]), nbr_neurone), dtype=tf.float32))
b=tf.Variable(np.zeros(shape=(nbr_neurone)), dtype=tf.float32)
result=tf.matmul(input, w)+b
if b_norm is True:
result=tf.layers.batch_normalization(result, training=training)
if f_activation is not None:
result=f_activation(result)
return result
def ia(nbr_classes, size, couche, learning_rate=1E-3):
ph_images=tf.placeholder(shape=(None, size, size, couche), dtype=tf.float32, name='entree')
ph_labels=tf.placeholder(shape=(None, nbr_classes), dtype=tf.float32)
ph_is_training=tf.placeholder_with_default(False, (), name='is_training')
result=convolution(ph_images, 3, 64, 1, True, tf.nn.relu, ph_is_training)
result=tf.layers.dropout(result, 0.3, training=ph_is_training)
result=convolution(result, 3, 128, 1, True, tf.nn.relu, ph_is_training)
result=tf.layers.dropout(result, 0.4, training=ph_is_training)
result=tf.nn.max_pool(result, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME')
result=tf.contrib.layers.flatten(result)
result=fc(result, 128, True, tf.nn.relu, ph_is_training)
result=tf.layers.dropout(result, 0.5, training=ph_is_training)
result=fc(result, nbr_classes)
socs=tf.nn.softmax(result, name="sortie")
loss=tf.nn.softmax_cross_entropy_with_logits_v2(labels=ph_labels, logits=result)
extra_update_ops=tf.get_collection(tf.GraphKeys.UPDATE_OPS)
with tf.control_dependencies(extra_update_ops):
train=tf.train.AdamOptimizer(learning_rate).minimize(loss)
accuracy=tf.reduce_mean(tf.cast(tf.equal(tf.argmax(socs, 1), tf.argmax(ph_labels, 1)), tf.float32))
return ph_images, ph_labels, ph_is_training, socs, train, accuracy, tf.train.Saver()
tab_images=[]
tab_labels=[]
for dir in ["/usr/share/fonts/truetype/ubuntu-font-family/", "/usr/share/fonts/truetype/freefont/"]:
for root, dirs, files in os.walk(dir):
for file in files:
if file.endswith("ttf"):
print(root+"/"+file)
for i in range(1, 10):
for cpt in range(nbr):
image=Image.new("L", (28, 28))
draw=ImageDraw.Draw(image)
font=ImageFont.truetype(root+"/"+file, np.random.randint(26, 32))
text="{:d}".format(i)
draw.text((np.random.randint(1, 10), np.random.randint(-4, 0)), text, font=font, fill=(255))
image=np.array(image).reshape(28, 28, 1)
tab_images.append(image)
tab_labels.append(np.eye(10)[i])
image_m=modif_image(image, 1.05+np.random.rand())
tab_images.append(image_m)
tab_labels.append(np.eye(10)[i])
image=np.zeros((28, 28, 1))
for cpt in range(3*nbr):
image_m=modif_image(image, 1.05+np.random.rand())
tab_images.append(image_m)
tab_labels.append(np.eye(10)[0])
tab_images=np.array(tab_images)
tab_labels=np.array(tab_labels)
tab_images=tab_images/255
tab_images, tab_labels=shuffle(tab_images, tab_labels)
if False: # Changer en True si vous voulez voir les images générées
for i in range(len(tab_images)):
cv2.imshow('chiffre', tab_images[i].reshape(28, 28, 1))
print(tab_labels[i], np.argmax(tab_labels[i]))
if cv2.waitKey()&0xFF==ord('q'):
break
print("Nbr:", len(tab_images))
train_images, test_images, train_labels, test_labels=train_test_split(tab_images, tab_labels, test_size=0.10)
images, labels, is_training, sortie, train, accuracy, saver=ia(10, 28, 1)
with tf.Session() as s:
s.run(tf.global_variables_initializer())
tab_train=[]
tab_test=[]
for id_entrainement in np.arange(nbr_entrainement):
print("> Entrainement", id_entrainement)
for batch in np.arange(0, len(train_images), taille_batch):
s.run(train, feed_dict={
images: train_images[batch:batch+taille_batch],
labels: train_labels[batch:batch+taille_batch],
is_training: True
})
print(" entrainement OK")
tab_accuracy_train=[]
for batch in np.arange(0, len(train_images), taille_batch):
p=s.run(accuracy, feed_dict={
images: train_images[batch:batch+taille_batch],
labels: train_labels[batch:batch+taille_batch],
is_training: True
})
tab_accuracy_train.append(p)
print(" train:", np.mean(tab_accuracy_train))
tab_accuracy_test=[]
for batch in np.arange(0, len(test_images), taille_batch):
p=s.run(accuracy, feed_dict={
images: test_images[batch:batch+taille_batch],
labels: test_labels[batch:batch+taille_batch],
is_training: True
})
tab_accuracy_test.append(p)
print(" test :", np.mean(tab_accuracy_test))
tab_train.append(1-np.mean(tab_accuracy_train))
tab_test.append(1-np.mean(tab_accuracy_test))
saver.save(s, './mon_modele/modele')