-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
250 lines (200 loc) · 10.1 KB
/
main.py
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
import cv2
from cvzone.HandTrackingModule import HandDetector
from extract import extract_text_from_image
from PIL import Image
import time
# Initialize webcam
cam = cv2.VideoCapture(0)
cam.set(3, 640)
cam.set(4, 480)
# Detect Hand
detector = HandDetector(maxHands=1)
# Video recording variables
is_recording = False
video_writer = None
video_filename = 'recorded_video.mp4'
# Camera Frame Variable
# Define the region where the frame will be inserted
top_left_y = 145
bottom_right_y = 580
top_left_x = 111
bottom_right_x = 576
# Calculate the size of the region where the frame will be inserted
region_height = bottom_right_y - top_left_y
region_width = bottom_right_x - top_left_x
# Instruction are Displaying
instruction = True
instruction_image = cv2.imread('resources/instructions.png')
# Display Text On Screen
extract_text = None
display_text = False
# Ask For Form Type
student_card = False
challan = False
otherForm = False
detect_hand = False
timer = 0
stateResult = False
# Main loop
while True:
# Load background image
bgImg = cv2.imread("resources/bg.png")
ret, frame = cam.read()
if not ret:
break
# Detect hands if game is running
hands, image = detector.findHands(frame) if detect_hand else (None, None)
if detect_hand:
if not stateResult:
timer = time.time() - initialTime
cv2.putText(bgImg, f"{int(4 - timer)}", (625,365), cv2.FONT_HERSHEY_PLAIN, 4, (255,0,255), 3)
if timer > 3: # Timer for the round
timer = 0
stateResult = True
# Player move
if hands:
hand = hands[0]
fingers = detector.fingersUp(hand)
if fingers == [0, 0, 0, 0, 0]:
student_card = False
challan = False
otherForm = True
resultText = "Selected Mode General Form!"
elif fingers == [1, 1, 1, 1, 1]:
student_card = True
challan = False
otherForm = False
resultText = "Selected Mode Student Card!"
elif fingers == [0, 1, 1, 0, 0]:
student_card = False
challan = True
otherForm = False
resultText = "Selected Mode Fee Chalan!"
else:
instruction = True
student_card = None
challan = None
otherForm = None
resultText = "Invalid Move!" # Show invalid move message
else:
instruction = True
student_card = None
challan = None
otherForm = None # No hand detected
resultText = "No Hand Detected!" # No hand detected message
# After detecting a move, reset state for next detection
if stateResult:
cv2.putText(bgImg, f"{resultText}", (715,380), cv2.FONT_HERSHEY_SIMPLEX, 0.8, (255, 0, 255), 4)
detect_hand = False # Stop detection until hand is needed again
# Display Text On Screen
if display_text:
# Set the text color to orange in BGR format
orange_color = (0, 165, 255)
# Update scores and instructions
cv2.putText(bgImg, f"Name:", (715,180), cv2.FONT_HERSHEY_SIMPLEX, 0.8, (255, 0, 255), 4)
cv2.putText(bgImg, f"{extract_text['Name']}", (815,180), cv2.FONT_HERSHEY_SIMPLEX, 0.62, orange_color, 1)
# Update scores and instructions
cv2.putText(bgImg, f"F. Name:", (715,210), cv2.FONT_HERSHEY_SIMPLEX, 0.8, (255, 0, 255), 4)
cv2.putText(bgImg, f"{extract_text["Father's Name"]}", (830,210), cv2.FONT_HERSHEY_SIMPLEX, 0.62, orange_color, 1)
if student_card:
cv2.putText(bgImg, f"Reg #:", (715,240), cv2.FONT_HERSHEY_SIMPLEX, 0.8, (255, 0, 255), 4)
cv2.putText(bgImg, f"{extract_text['Reg #']}", (820,240), cv2.FONT_HERSHEY_SIMPLEX, 0.62, orange_color, 1)
cv2.putText(bgImg, f"Department:", (715,270), cv2.FONT_HERSHEY_SIMPLEX, 0.8, (255, 0, 255), 4)
cv2.putText(bgImg, f"{extract_text['Department']}", (820,270), cv2.FONT_HERSHEY_SIMPLEX, 0.62, orange_color, 1)
# Update scores and instructions
cv2.putText(bgImg, f"Subject:", (715,300), cv2.FONT_HERSHEY_SIMPLEX, 0.8, (255, 0, 255), 4)
cv2.putText(bgImg, f"{extract_text['Choice of Subject']}", (820,300), cv2.FONT_HERSHEY_SIMPLEX, 0.62, orange_color, 1)
elif otherForm:
cv2.putText(bgImg, f"CNIC:", (715,240), cv2.FONT_HERSHEY_SIMPLEX, 0.8, (255, 0, 255), 4)
cv2.putText(bgImg, f"{extract_text['CNIC/B.Form No']}", (820,240), cv2.FONT_HERSHEY_SIMPLEX, 0.62, orange_color, 1)
cv2.putText(bgImg, f"Domicile:", (715,270), cv2.FONT_HERSHEY_SIMPLEX, 0.8, (255, 0, 255), 4)
cv2.putText(bgImg, f"{extract_text['Domicile']}", (820,270), cv2.FONT_HERSHEY_SIMPLEX, 0.62, orange_color, 1)
if not student_card:
if challan:
cv2.putText(bgImg, f"P. No:", (715,240), cv2.FONT_HERSHEY_SIMPLEX, 0.8, (255, 0, 255), 4)
cv2.putText(bgImg, f"{extract_text['Phone No']}", (820,240), cv2.FONT_HERSHEY_SIMPLEX, 0.62, orange_color, 1)
cv2.putText(bgImg, f"Form No:", (715,270), cv2.FONT_HERSHEY_SIMPLEX, 0.8, (255, 0, 255), 4)
cv2.putText(bgImg, f"{extract_text['Form No']}", (855,270), cv2.FONT_HERSHEY_SIMPLEX, 0.62, orange_color, 1)
cv2.putText(bgImg, f"Address:", (715,300), cv2.FONT_HERSHEY_SIMPLEX, 0.8, (255, 0, 255), 4)
cv2.putText(bgImg, f"{extract_text['Address']}", (855,300), cv2.FONT_HERSHEY_SIMPLEX, 0.62, orange_color, 1)
else:
cv2.putText(bgImg, f"P. No:", (715,330), cv2.FONT_HERSHEY_SIMPLEX, 0.8, (255, 0, 255), 4)
cv2.putText(bgImg, f"{extract_text['Phone No']}", (820,330), cv2.FONT_HERSHEY_SIMPLEX, 0.62, orange_color, 1)
cv2.putText(bgImg, f"Form No:", (715,390), cv2.FONT_HERSHEY_SIMPLEX, 0.8, (255, 0, 255), 4)
cv2.putText(bgImg, f"{extract_text['Form No']}", (855,390), cv2.FONT_HERSHEY_SIMPLEX, 0.62, orange_color, 1)
cv2.putText(bgImg, f"Address:", (715,300), cv2.FONT_HERSHEY_SIMPLEX, 0.8, (255, 0, 255), 4)
cv2.putText(bgImg, f"{extract_text['Address']}", (855,300), cv2.FONT_HERSHEY_SIMPLEX, 0.62, orange_color, 1)
if otherForm:
cv2.putText(bgImg, f"Gender:", (715,360), cv2.FONT_HERSHEY_SIMPLEX, 0.8, (255, 0, 255), 4)
cv2.putText(bgImg, f"{extract_text['Gender']}", (840,360), cv2.FONT_HERSHEY_SIMPLEX, 0.62, orange_color, 1)
cv2.putText(bgImg, f"City:", (715,420), cv2.FONT_HERSHEY_SIMPLEX, 0.8, (255, 0, 255), 4)
cv2.putText(bgImg, f"{extract_text['City']}", (820,420), cv2.FONT_HERSHEY_SIMPLEX, 0.62, orange_color, 1)
cv2.putText(bgImg, f"Postal Code:", (715,450), cv2.FONT_HERSHEY_SIMPLEX, 0.8, (255, 0, 255), 4)
cv2.putText(bgImg, f"{extract_text['Postal Code']}", (865,450), cv2.FONT_HERSHEY_SIMPLEX, 0.62, orange_color, 1)
cv2.putText(bgImg, f"Category:", (715,480), cv2.FONT_HERSHEY_SIMPLEX, 0.8, (255, 0, 255), 4)
cv2.putText(bgImg, f"{extract_text['Category']}", (820,480), cv2.FONT_HERSHEY_SIMPLEX, 0.62, orange_color, 1)
# Update scores and instructions
cv2.putText(bgImg, f"Subject:", (715,510), cv2.FONT_HERSHEY_SIMPLEX, 0.8, (255, 0, 255), 4)
cv2.putText(bgImg, f"{extract_text['Choice of Subject']}", (820,510), cv2.FONT_HERSHEY_SIMPLEX, 0.62, orange_color, 1)
# Store the original frame for processing
original_frame = frame.copy()
# Resize and crop webcam feed
frame = cv2.resize(frame, (0, 0), fx=0.95, fy=0.91)
frame = frame[80:540, 85:520]
if not instruction:
frame = cv2.resize(frame, (region_width, region_height))
else:
frame = cv2.resize(instruction_image, (region_width, region_height))
# Apply the Summer DeepGreen
frame = cv2.applyColorMap(frame, cv2.COLORMAP_DEEPGREEN)
# Overlay the webcam feed
bgImg[top_left_y:bottom_right_y, top_left_x:bottom_right_x] = frame
bgImg = cv2.resize(bgImg, (950, 650))
cv2.imshow("Form Digitilization Using CV2 And AI", bgImg)
# Video recording logic
if is_recording:
video_writer.write(bgImg)
# Key press handling
key = cv2.waitKey(1) & 0xFF
if key == ord("q"):
break
elif key == ord('i'):
stateResult = False
display_text = False
instruction = True
elif key == ord('c'):
stateResult = False
instruction = False
display_text = False
elif key == ord('s') and not instruction:
# Convert the frame to grayscale for OCR
stateResult = False
gray_frame = cv2.cvtColor(original_frame, cv2.COLOR_BGR2GRAY)
pil_image = Image.fromarray(gray_frame)
extract_text = extract_text_from_image(pil_image)
display_text = True
elif key == ord('r'):
stateResult = False
display_text = False
elif key == ord('h') and not instruction:
detect_hand = True
stateResult = False
initialTime = time.time()
display_text = False
elif key == ord('p'): # Toggle recording with 'p'
if not is_recording:
# Start recording
is_recording = True
video_writer = cv2.VideoWriter(video_filename, cv2.VideoWriter_fourcc(*'XVID'), 20.0, (950, 650))
print("Recording started...")
else:
# Stop recording
is_recording = False
video_writer.release()
video_writer = None
print("Recording stopped.")
# Release resources
cam.release()
if video_writer:
video_writer.release()
cv2.destroyAllWindows()