-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathImageToText.py
More file actions
64 lines (51 loc) · 1.69 KB
/
ImageToText.py
File metadata and controls
64 lines (51 loc) · 1.69 KB
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
import cv2
import pytesseract
import re
import numpy as np
# Function to capture an image from the webcam
def capture_image():
cap = cv2.VideoCapture(0)
if not cap.isOpened():
print("Error: Couldn't open the webcam")
return None
while True:
# Read a frame from the webcam
ret, frame = cap.read()
if not ret:
print("Error: Couldn't capture a frame")
break
# Display the frame from the webcam
cv2.imshow("Webcam", frame)
# Check for the 'q' key press
key = cv2.waitKey(1)
if key == ord('q'):
# Capture the current frame in memory (NumPy array)
captured_frame = frame.copy()
print("Image captured temporarily")
break
cap.release()
cv2.destroyAllWindows()
if captured_frame is not None:
extracted_text = extract_text(captured_frame)
if extracted_text:
# Extract Aadhar number
aadhar_number = extract_aadhar_number(extracted_text)
if aadhar_number:
print("Extracted Aadhar Number:", aadhar_number)
else:
print("Aadhar number not found in the text")
def extract_text(frame):
try:
text = pytesseract.image_to_string(frame)
return text
except Exception as e:
print(f"Error during text extraction: {str(e)}")
return None
def extract_aadhar_number(text):
aadhar_pattern = r'\d{4}[\s-]?\d{4}[\s-]?\d{4}'
aadhar_match = re.search(aadhar_pattern, text)
if aadhar_match:
return aadhar_match.group().replace(" ", "").replace("-", "")
return None
if __name__ == "__main__":
capture_image()