Spaces:
Running
Running
Update passport_img.py
Browse files- passport_img.py +157 -1
passport_img.py
CHANGED
@@ -1,6 +1,12 @@
|
|
1 |
import cv2
|
2 |
import numpy as np
|
3 |
import matplotlib.pyplot as plt
|
|
|
|
|
|
|
|
|
|
|
|
|
4 |
|
5 |
|
6 |
|
@@ -94,4 +100,154 @@ def merge_all(number,input_path,size):
|
|
94 |
# print(merge_img_final.shape)
|
95 |
# cv2.imwrite(output_path, merge_img_final)
|
96 |
# print(merge_img_final.shape)
|
97 |
-
return merge_img_final
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import cv2
|
2 |
import numpy as np
|
3 |
import matplotlib.pyplot as plt
|
4 |
+
import ultralytics
|
5 |
+
from ultralytics import YOLO
|
6 |
+
print(ultralytics.__version__)
|
7 |
+
from ultralytics.yolo.utils.ops import scale_image
|
8 |
+
model = YOLO('yolov8n-face.pt')
|
9 |
+
model_seg = YOLO('yolov8s-seg.pt')
|
10 |
|
11 |
|
12 |
|
|
|
100 |
# print(merge_img_final.shape)
|
101 |
# cv2.imwrite(output_path, merge_img_final)
|
102 |
# print(merge_img_final.shape)
|
103 |
+
return merge_img_final
|
104 |
+
|
105 |
+
def align_crop_image(img):
|
106 |
+
# img = cv2.imread(img)
|
107 |
+
img = cv2.copyMakeBorder(img, 50, 0, 0, 0, cv2.BORDER_CONSTANT, value=(255,255,255))
|
108 |
+
results = model(img)
|
109 |
+
if len(results[0].boxes.data)==1:
|
110 |
+
b_box_up = int(results[0].boxes.data[0][0]),int(results[0].boxes.data[0][1])
|
111 |
+
b_box_down = int(results[0].boxes.data[0][2]),int(results[0].boxes.data[0][3])
|
112 |
+
left_eye=int(results[0].keypoints.data[0][0][0]),int(results[0].keypoints.data[0][0][1])
|
113 |
+
right_eye=int(results[0].keypoints.data[0][1][0]),int(results[0].keypoints.data[0][1][1])
|
114 |
+
nose=int(results[0].keypoints.data[0][2][0]),int(results[0].keypoints.data[0][2][1])
|
115 |
+
left_lip=int(results[0].keypoints.data[0][3][0]),int(results[0].keypoints.data[0][3][1])
|
116 |
+
right_lip=int(results[0].keypoints.data[0][4][0]),int(results[0].keypoints.data[0][4][1])
|
117 |
+
left_eye_center = left_eye
|
118 |
+
left_eye_x = left_eye_center[0]
|
119 |
+
left_eye_y = left_eye_center[1]
|
120 |
+
|
121 |
+
right_eye_center = right_eye
|
122 |
+
right_eye_x = right_eye_center[0]
|
123 |
+
right_eye_y = right_eye_center[1]
|
124 |
+
|
125 |
+
if left_eye_y > right_eye_y:
|
126 |
+
A = (right_eye_x, left_eye_y)
|
127 |
+
# Integer -1 indicates that the image will rotate in the clockwise direction
|
128 |
+
direction = -1
|
129 |
+
else:
|
130 |
+
A = (left_eye_x, right_eye_y)
|
131 |
+
# Integer 1 indicates that image will rotate in the counter clockwise
|
132 |
+
# direction
|
133 |
+
direction = 1
|
134 |
+
delta_x = right_eye_x - left_eye_x
|
135 |
+
delta_y = right_eye_y - left_eye_y
|
136 |
+
angle=np.arctan(delta_y/delta_x)
|
137 |
+
angle = (angle * 180) / np.pi
|
138 |
+
h, w = img.shape[:2]
|
139 |
+
# Calculating a center point of the image
|
140 |
+
# Integer division "//"" ensures that we receive whole numbers
|
141 |
+
center = (w // 2, h // 2)
|
142 |
+
# Defining a matrix M and calling
|
143 |
+
# cv2.getRotationMatrix2D method
|
144 |
+
M = cv2.getRotationMatrix2D(center, (angle), 1.0)
|
145 |
+
# Applying the rotation to our image using the
|
146 |
+
# cv2.warpAffine method
|
147 |
+
rotated = cv2.warpAffine(img, M, (w, h))
|
148 |
+
results = model(rotated)
|
149 |
+
b_box_up = int(results[0].boxes.data[0][0]),int(results[0].boxes.data[0][1])
|
150 |
+
b_box_down = int(results[0].boxes.data[0][2]),int(results[0].boxes.data[0][3])
|
151 |
+
box_height = b_box_down[0]-b_box_up[0]
|
152 |
+
box_width = b_box_down[1]-b_box_up[1]
|
153 |
+
left_eye=int(results[0].keypoints.data[0][0][0]),int(results[0].keypoints.data[0][0][1])
|
154 |
+
right_eye=int(results[0].keypoints.data[0][1][0]),int(results[0].keypoints.data[0][1][1])
|
155 |
+
nose=int(results[0].keypoints.data[0][2][0]),int(results[0].keypoints.data[0][2][1])
|
156 |
+
left_lip=int(results[0].keypoints.data[0][3][0]),int(results[0].keypoints.data[0][3][1])
|
157 |
+
right_lip=int(results[0].keypoints.data[0][4][0]),int(results[0].keypoints.data[0][4][1])
|
158 |
+
left_eye_center = left_eye
|
159 |
+
left_eye_x = left_eye_center[0]
|
160 |
+
left_eye_y = left_eye_center[1]
|
161 |
+
|
162 |
+
right_eye_center = right_eye
|
163 |
+
right_eye_x = right_eye_center[0]
|
164 |
+
right_eye_y = right_eye_center[1]
|
165 |
+
|
166 |
+
up_crop = b_box_up[0]-int(box_height/2),b_box_up[1]-int(box_width/3)
|
167 |
+
down_crop = b_box_down[0]+int(box_height/2),right_lip[1]+box_width
|
168 |
+
final_image = rotated[up_crop[1]:down_crop[1],up_crop[0]:down_crop[0]]
|
169 |
+
resized_image =cv2.resize(final_image, (210, 297))
|
170 |
+
return resized_image
|
171 |
+
else:
|
172 |
+
return None
|
173 |
+
|
174 |
+
|
175 |
+
def predict_on_image(img, conf):
|
176 |
+
result = model_seg(img, conf=conf)[0]
|
177 |
+
|
178 |
+
# detection
|
179 |
+
# result.boxes.xyxy # box with xyxy format, (N, 4)
|
180 |
+
cls = result.boxes.cls.cpu().numpy() # cls, (N, 1)
|
181 |
+
probs = result.boxes.conf.cpu().numpy() # confidence score, (N, 1)
|
182 |
+
boxes = result.boxes.xyxy.cpu().numpy() # box with xyxy format, (N, 4)
|
183 |
+
|
184 |
+
# segmentation
|
185 |
+
if result.masks:
|
186 |
+
masks = result.masks.data.cpu().numpy() # masks, (N, H, W)
|
187 |
+
masks = np.moveaxis(masks, 0, -1) # masks, (H, W, N)
|
188 |
+
print(masks.shape)
|
189 |
+
# rescale masks to original image
|
190 |
+
masks = scale_image(masks, result.masks.orig_shape)
|
191 |
+
masks = np.moveaxis(masks, -1, 0) # masks, (N, H, W)
|
192 |
+
|
193 |
+
return boxes, masks, cls, probs
|
194 |
+
else:
|
195 |
+
return None
|
196 |
+
|
197 |
+
|
198 |
+
def overlay(image, mask, color, alpha, resize=None):
|
199 |
+
"""Combines image and its segmentation mask into a single image.
|
200 |
+
https://www.kaggle.com/code/purplejester/showing-samples-with-segmentation-mask-overlay
|
201 |
+
|
202 |
+
Params:
|
203 |
+
image: Training image. np.ndarray,
|
204 |
+
mask: Segmentation mask. np.ndarray,
|
205 |
+
color: Color for segmentation mask rendering. tuple[int, int, int] = (255, 0, 0)
|
206 |
+
alpha: Segmentation mask's transparency. float = 0.5,
|
207 |
+
resize: If provided, both image and its mask are resized before blending them together.
|
208 |
+
tuple[int, int] = (1024, 1024))
|
209 |
+
|
210 |
+
Returns:
|
211 |
+
image_combined: The combined image. np.ndarray
|
212 |
+
|
213 |
+
"""
|
214 |
+
for i in range(len(mask)):
|
215 |
+
for j in range(len(mask[i])):
|
216 |
+
# print(masks[0][i][j])
|
217 |
+
mask[i][j] = abs(mask[i][j] - 1)
|
218 |
+
color = color[::-1]
|
219 |
+
colored_mask = np.expand_dims(mask, 0).repeat(3, axis=0)
|
220 |
+
colored_mask = np.moveaxis(colored_mask, 0, -1)
|
221 |
+
masked = np.ma.MaskedArray(image, mask=colored_mask, fill_value=color)
|
222 |
+
image_overlay = masked.filled()
|
223 |
+
if resize is not None:
|
224 |
+
image = cv2.resize(image.transpose(1, 2, 0), resize)
|
225 |
+
image_overlay = cv2.resize(image_overlay.transpose(1, 2, 0), resize)
|
226 |
+
|
227 |
+
image_combined = cv2.addWeighted(image, 1 - alpha, image_overlay, alpha, 0)
|
228 |
+
|
229 |
+
return image_combined
|
230 |
+
|
231 |
+
def add_name_dob(img,name,dob):
|
232 |
+
img = cv2.resize(img,(420,594))
|
233 |
+
img = cv2.rectangle(img,(0,520),(420,594),(255,255,255),-1)
|
234 |
+
dob = 'D.O.B: '+dob
|
235 |
+
font = 1#cv2.FONT_HERSHEY_SIMPLEX
|
236 |
+
font_scale = 2
|
237 |
+
font_thickness = 2
|
238 |
+
|
239 |
+
# Get the size of the text
|
240 |
+
text_size = cv2.getTextSize(name, font, font_scale, font_thickness)[0]
|
241 |
+
dob_size = cv2.getTextSize(dob, font, font_scale, font_thickness)[0]
|
242 |
+
|
243 |
+
# Calculate the position to center the text
|
244 |
+
text_x = (img.shape[1] - text_size[0]) // 2
|
245 |
+
text_y = (img.shape[0] + text_size[1]) // 2
|
246 |
+
dob_x = (img.shape[1] - dob_size[0]) // 2
|
247 |
+
dob_y = (img.shape[0] + dob_size[1]) // 2
|
248 |
+
|
249 |
+
# Put the text on the image
|
250 |
+
cv2.putText(img, name, (text_x, 547), font, font_scale, (0, 0, 0), font_thickness)
|
251 |
+
cv2.putText(img, dob, (dob_x, 583), font, font_scale, (0, 0, 0), font_thickness)
|
252 |
+
|
253 |
+
return img
|