File size: 1,802 Bytes
8565879
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
59
60
61
62
63
64
import cv2
import base64
from utils import *

CIN =  ["Nom complet", "Date de naissance", "Date de validité", "Lieu de naissance", "Numéro CIN"]



def extract_face(image, scale_factor=1.2):
    # Load the pre-trained face detector
    face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_alt.xml')


    # Convert the image to grayscale
    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

    # Detect faces in the image
    faces = face_cascade.detectMultiScale(gray, scaleFactor=scale_factor, minNeighbors=5, minSize=(30, 30))
    print(len(faces))
    if len(faces) == 0:
        print("No faces found")
        return None

    # Assume only one face in the image for simplicity
    (x, y, w, h) = max(faces, key=lambda x: x[2])

    # Expand the bounding box to include a slightly larger region
    x -= int(0.1 * w)
    y -= int(0.1 * h)
    w += int(0.2 * w)
    h += int(0.2 * h)

    # Ensure the coordinates are within the image boundaries
    x = max(x, 0)
    y = max(y, 0)
    w = min(w, image.shape[1])
    h = min(h, image.shape[0])

    # Extract the face region from the image
    face = image[y:y+h, x:x+w]

    # Convert the face to base64 string
    _, encoded_image = cv2.imencode('.jpg', face)
    encoded_image_str = base64.b64encode(encoded_image).decode('utf-8')

    # Create a dictionary to store image information
    image_info = {
        "width": face.shape[1],
        "height": face.shape[0],
        "data": encoded_image_str
    }

    return image_info
   

if __name__ == '__main__':
    lang = "french"
    to_be_extracted = CIN
    image_path = "./docs for ocr/CIN 2.png"
    image = cv2.imread(image_path)
    
    text_data = extract_data(lang, to_be_extracted, image)
    print(text_data)
    face_data = extract_face(image)