Best Python code snippet using lemoncheesecake
split_eyes_mouth.py
Source:split_eyes_mouth.py
1import sys2import cv23import dlib4import numpy as np5detector = dlib.get_frontal_face_detector()6predictor = dlib.shape_predictor("shape_predictor_68_face_landmarks.dat")7COLOR_WHITE = (255, 255, 255)8img_size_width = 3009img_size_height = 30010''''''''''''''''''''' Realtime Webcam Code '''''''''''''''''''''''11cap = cv2.VideoCapture(0)12masked_img = np.zeros((img_size_width, img_size_height, 3), np.uint8)13while True:14 ret, img = cap.read()15 if not ret:16 break17 org_img = cv2.resize(img, dsize=(img_size_width, img_size_height), interpolation=cv2.INTER_LINEAR)18 draw_img = np.zeros((org_img.shape[0], org_img.shape[1], org_img.shape[2]), np.uint8)19 gray = cv2.cvtColor(org_img, cv2.COLOR_BGR2GRAY)20 faces = detector(gray, 1)21 print("Number of Faces Detected: ", len(faces))22 roi_parts = []23 for face in faces:24 landmarks = predictor(gray, face)25 left_eye_points = []26 for i in range(36, 42):27 point = [landmarks.part(i).x, landmarks.part(i).y]28 left_eye_points.append(point)29 left_eye_mask = np.array(left_eye_points, dtype=np.int32)30 img_line = cv2.polylines(draw_img, [left_eye_mask], True, COLOR_WHITE, thickness=2, lineType=cv2.LINE_8)31 draw_img = cv2.fillPoly(img_line, [left_eye_mask], COLOR_WHITE, lineType=cv2.LINE_AA)32 right_eye_points = []33 for i in range(42, 48):34 point = [landmarks.part(i).x, landmarks.part(i).y]35 right_eye_points.append(point)36 right_eye_mask = np.array(right_eye_points, dtype=np.int32)37 img_line = cv2.polylines(draw_img, [right_eye_mask], True, COLOR_WHITE, thickness=2, lineType=cv2.LINE_8)38 draw_img = cv2.fillPoly(img_line, [right_eye_mask], COLOR_WHITE, lineType=cv2.LINE_AA)39 mouth_points = []40 for i in range(48, 60):41 point = [landmarks.part(i).x, landmarks.part(i).y]42 mouth_points.append(point)43 mouth_mask = np.array(mouth_points, dtype=np.int32)44 img_line = cv2.polylines(draw_img, [mouth_mask], True, COLOR_WHITE, thickness=2, lineType=cv2.LINE_8)45 draw_img = cv2.fillPoly(img_line, [mouth_mask], COLOR_WHITE, lineType=cv2.LINE_AA)46 masked_img = cv2.bitwise_and(org_img, draw_img)47 draw_img_gray = cv2.cvtColor(draw_img, cv2.COLOR_BGR2GRAY)48 contours, hierarchy = cv2.findContours(draw_img_gray, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)49 roi_list = []50 for contour in contours:51 x, y, w, h = cv2.boundingRect(contour)52 cv2.rectangle(draw_img, (x, y), (x + w, y + h), (0, 80, 0), 2)53 roi = masked_img[y:y + h, x:x + w]54 roi = cv2.resize(roi, dsize=(img_size_width // 1, img_size_height // 1), interpolation=cv2.INTER_LINEAR)55 roi_list.append(roi)56 if len(contours) > 2:57 roi_left_eye = roi_list[1]58 roi_right_eye = roi_list[2]59 if cv2.boundingRect(contours[1])[0] > cv2.boundingRect(contours[2])[0]:60 roi_left_eye = roi_list[2]61 roi_right_eye = roi_list[1]62 roi_parts.append(roi_list[0])63 roi_parts.append(roi_left_eye)64 roi_parts.append(roi_right_eye)65 cv2.imshow('org_img', org_img)66 cv2.imshow('contoured_img', draw_img)67 cv2.imshow('masked_img', masked_img)68 if len(roi_parts) > 0 and len(roi_parts) % 3 == 0:69 for roi_idx in range(0, len(roi_parts), 3):70 cv2.imshow('roi_mouth', roi_parts[roi_idx])71 cv2.imshow('roi_left_eye', roi_parts[roi_idx + 1])72 cv2.imshow('roi_right_eye', roi_parts[roi_idx + 2])73 if cv2.waitKey(1) == ord('q'):74 sys.exit(1)75# ''''''''''''''''''''' Image Code '''''''''''''''''''''''76# # org_img = cv2.imread('person.jpg')77# org_img = cv2.imread('people.jpg')78#79# if org_img is None:80# print('File Not Available')81# exit(0)82#83# org_img = cv2.resize(org_img, dsize=(img_size_width, img_size_height), interpolation=cv2.INTER_LINEAR)84# draw_img = np.zeros((org_img.shape[0], org_img.shape[1], org_img.shape[2]), np.uint8)85# gray = cv2.cvtColor(org_img, cv2.COLOR_BGR2GRAY)86# faces = detector(gray, 1)87# masked_img = np.zeros((img_size_width, img_size_height, 3), np.uint8)88# print("Number of Faces Detected: ", len(faces))89#90# roi_parts = []91#92# for face in faces:93# landmarks = predictor(gray, face)94#95# left_eye_points = []96# for i in range(36, 42):97# point = [landmarks.part(i).x, landmarks.part(i).y]98# left_eye_points.append(point)99#100# left_eye_mask = np.array(left_eye_points, dtype=np.int32)101# img_line = cv2.polylines(draw_img, [left_eye_mask], True, COLOR_WHITE, thickness=2, lineType=cv2.LINE_8)102# draw_img = cv2.fillPoly(img_line, [left_eye_mask], COLOR_WHITE, lineType=cv2.LINE_AA)103#104# right_eye_points = []105# for i in range(42, 48):106# point = [landmarks.part(i).x, landmarks.part(i).y]107# right_eye_points.append(point)108#109# right_eye_mask = np.array(right_eye_points, dtype=np.int32)110# img_line = cv2.polylines(draw_img, [right_eye_mask], True, COLOR_WHITE, thickness=2, lineType=cv2.LINE_8)111# draw_img = cv2.fillPoly(img_line, [right_eye_mask], COLOR_WHITE, lineType=cv2.LINE_AA)112#113# masked_img = cv2.bitwise_and(org_img, draw_img)114#115# mouth_points = []116# for i in range(48, 60):117# point = [landmarks.part(i).x, landmarks.part(i).y]118# mouth_points.append(point)119#120# mouth_mask = np.array(mouth_points, dtype=np.int32)121# img_line = cv2.polylines(draw_img, [mouth_mask], True, COLOR_WHITE, thickness=2, lineType=cv2.LINE_8)122# draw_img = cv2.fillPoly(img_line, [mouth_mask], COLOR_WHITE, lineType=cv2.LINE_AA)123#124# draw_img_gray = cv2.cvtColor(draw_img, cv2.COLOR_BGR2GRAY)125# contours, hierarchy = cv2.findContours(draw_img_gray, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)126# roi_list = []127#128# for contour in contours:129# x, y, w, h = cv2.boundingRect(contour)130# roi = masked_img[y:y + h, x:x + w]131# # cv2.rectangle(draw_img, (x, y), (x + w, y + h), (0, 255, 0), 2)132# roi = cv2.resize(roi, dsize=(img_size_width // 1, img_size_height // 1), interpolation=cv2.INTER_LINEAR)133# roi_list.append(roi)134#135# if len(contours) > 2:136# roi_left_eye = roi_list[1]137# roi_right_eye = roi_list[2]138#139# if cv2.boundingRect(contours[1])[0] > cv2.boundingRect(contours[2])[0]:140# roi_left_eye = roi_list[2]141# roi_right_eye = roi_list[1]142#143# roi_parts.append(roi_list[0])144# roi_parts.append(roi_left_eye)145# roi_parts.append(roi_right_eye)146#147# if len(roi_parts) > 0 and len(roi_parts) % 3 == 0:148# for roi_idx in range(0, len(roi_parts), 3):149# save_image_file = 'roi_idx%d_mouth.jpg' % (roi_idx // 3)150# print("Save Image:", save_image_file)151# cv2.imwrite(save_image_file, roi_parts[roi_idx])152#153# save_image_file = 'roi_idx%d_left_eye.jpg' % (roi_idx // 3)154# print("Save Image:", save_image_file)155# cv2.imwrite(save_image_file, roi_parts[roi_idx + 1])156#157# save_image_file = 'roi_idx%d_right_eye.jpg' % (roi_idx // 3)158# print("Save Image:", save_image_file)...
contour_eyes_mouth.py
Source:contour_eyes_mouth.py
1import os2import sys3import cv24import dlib5import numpy as np6detector = dlib.get_frontal_face_detector()7predictor = dlib.shape_predictor("shape_predictor_68_face_landmarks.dat")8COLOR_WHITE = (255, 255, 255)9img_size_width = 30010img_size_height = 30011''''''''''''''''''''' Realtime Webcam Code '''''''''''''''''''''''12cap = cv2.VideoCapture(0)13masked_img = np.zeros((img_size_width, img_size_height, 3), np.uint8)14while True:15 ret, img = cap.read()16 if not ret:17 break18 org_img = cv2.resize(img, dsize=(img_size_width, img_size_height), interpolation=cv2.INTER_LINEAR)19 draw_img = np.zeros((org_img.shape[0], org_img.shape[1], org_img.shape[2]), np.uint8)20 gray = cv2.cvtColor(org_img, cv2.COLOR_BGR2GRAY)21 faces = detector(gray, 1)22 print("Number of Faces Detected: ", len(faces))23 for face in faces:24 landmarks = predictor(gray, face)25 left_eye_points = []26 for i in range(36, 42):27 point = [landmarks.part(i).x, landmarks.part(i).y]28 left_eye_points.append(point)29 left_eye_mask = np.array(left_eye_points, dtype=np.int32)30 img_line = cv2.polylines(draw_img, [left_eye_mask], True, COLOR_WHITE, thickness=2, lineType=cv2.LINE_8)31 draw_img = cv2.fillPoly(img_line, [left_eye_mask], COLOR_WHITE, lineType=cv2.LINE_AA)32 right_eye_points = []33 for i in range(42, 48):34 point = [landmarks.part(i).x, landmarks.part(i).y]35 right_eye_points.append(point)36 right_eye_mask = np.array(right_eye_points, dtype=np.int32)37 img_line = cv2.polylines(draw_img, [right_eye_mask], True, COLOR_WHITE, thickness=2, lineType=cv2.LINE_8)38 draw_img = cv2.fillPoly(img_line, [right_eye_mask], COLOR_WHITE, lineType=cv2.LINE_AA)39 mouth_points = []40 for i in range(48, 60):41 point = [landmarks.part(i).x, landmarks.part(i).y]42 mouth_points.append(point)43 mouth_mask = np.array(mouth_points, dtype=np.int32)44 img_line = cv2.polylines(draw_img, [mouth_mask], True, COLOR_WHITE, thickness=2, lineType=cv2.LINE_8)45 draw_img = cv2.fillPoly(img_line, [mouth_mask], COLOR_WHITE, lineType=cv2.LINE_AA)46 masked_img = cv2.bitwise_and(org_img, draw_img)47 draw_img_gray = cv2.cvtColor(draw_img, cv2.COLOR_BGR2GRAY)48 contours, hierarchy = cv2.findContours(draw_img_gray, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)49 for contour in contours:50 cv2.drawContours(draw_img, [contour], 0, (255, 0, 0), 3)51 for contour in contours:52 x, y, w, h = cv2.boundingRect(contour)53 cv2.rectangle(draw_img, (x, y), (x + w, y + h), (0, 255, 0), 2)54 cv2.imshow('org_img', org_img)55 cv2.imshow('contoured_img', draw_img)56 cv2.imshow('masked_img', masked_img)57 if cv2.waitKey(1) == ord('q'):58 sys.exit(1)59# ''''''''''''''''''''' Image Code '''''''''''''''''''''''60# # org_img = cv2.imread('person.jpg')61# org_img = cv2.imread('people.jpg')62#63# if org_img is None:64# print('File Not Available')65# exit(0)66#67# org_img = cv2.resize(org_img, dsize=(img_size_width, img_size_height), interpolation=cv2.INTER_LINEAR)68# draw_img = np.zeros((org_img.shape[0], org_img.shape[1], org_img.shape[2]), np.uint8)69# gray = cv2.cvtColor(org_img, cv2.COLOR_BGR2GRAY)70# faces = detector(gray, 1)71# print("Number of Faces Detected: ", len(faces))72#73# for face in faces:74# landmarks = predictor(gray, face)75#76# left_eye_points = []77# for i in range(36, 42):78# point = [landmarks.part(i).x, landmarks.part(i).y]79# left_eye_points.append(point)80#81# left_eye_mask = np.array(left_eye_points, dtype=np.int32)82# img_line = cv2.polylines(draw_img, [left_eye_mask], True, COLOR_WHITE, thickness=2, lineType=cv2.LINE_8)83# draw_img = cv2.fillPoly(img_line, [left_eye_mask], COLOR_WHITE, lineType=cv2.LINE_AA)84#85# right_eye_points = []86# for i in range(42, 48):87# point = [landmarks.part(i).x, landmarks.part(i).y]88# right_eye_points.append(point)89#90# right_eye_mask = np.array(right_eye_points, dtype=np.int32)91# img_line = cv2.polylines(draw_img, [right_eye_mask], True, COLOR_WHITE, thickness=2, lineType=cv2.LINE_8)92# draw_img = cv2.fillPoly(img_line, [right_eye_mask], COLOR_WHITE, lineType=cv2.LINE_AA)93#94# mouth_points = []95# for i in range(48, 60):96# point = [landmarks.part(i).x, landmarks.part(i).y]97# mouth_points.append(point)98#99# mouth_mask = np.array(mouth_points, dtype=np.int32)100# img_line = cv2.polylines(draw_img, [mouth_mask], True, COLOR_WHITE, thickness=2, lineType=cv2.LINE_8)101# draw_img = cv2.fillPoly(img_line, [mouth_mask], COLOR_WHITE, lineType=cv2.LINE_AA)102#103# draw_img_gray = cv2.cvtColor(draw_img, cv2.COLOR_BGR2GRAY)104# contours, hierarchy = cv2.findContours(draw_img_gray, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)105#106# for contour in contours:107# cv2.drawContours(draw_img, [contour], 0, (255, 0, 0), 3)108#109# for contour in contours:110# x, y, w, h = cv2.boundingRect(contour)111# cv2.rectangle(draw_img, (x, y), (x + w, y + h), (0, 255, 0), 2)112#113# contour_dir = 'contour_img'114# if not os.path.exists(contour_dir):115# os.makedirs(contour_dir)116#117# save_image_file = "org_img.jpg"118# print("Save Image:", save_image_file)119# cv2.imwrite(contour_dir + '/' + save_image_file, org_img)120#121# save_image_file = "binary_masked.jpg"122# print("Save Image:", save_image_file)123# cv2.imwrite(contour_dir + '/' + save_image_file, draw_img_gray)124#125# save_image_file = "draw_contour.jpg"126# print("Save Image:", save_image_file)...
masking_eyes_mouth.py
Source:masking_eyes_mouth.py
1import sys2import cv23import dlib4import numpy as np5detector = dlib.get_frontal_face_detector()6predictor = dlib.shape_predictor("shape_predictor_68_face_landmarks.dat")7COLOR_WHITE = (255, 255, 255)8img_size_width = 3009img_size_height = 30010''''''''''''''''''''' Realtime Webcam Code '''''''''''''''''''''''11cap = cv2.VideoCapture(0)12masked_img = np.zeros((img_size_width, img_size_height, 3), np.uint8)13while True:14 ret, img = cap.read()15 if not ret:16 break17 org_img = cv2.resize(img, dsize=(img_size_width, img_size_height), interpolation=cv2.INTER_LINEAR)18 draw_img = np.zeros((org_img.shape[0], org_img.shape[1], org_img.shape[2]), np.uint8)19 gray = cv2.cvtColor(org_img, cv2.COLOR_BGR2GRAY)20 faces = detector(gray, 1)21 print("Number of Faces Detected: ", len(faces))22 for face in faces:23 landmarks = predictor(gray, face)24 left_eye_points = []25 for i in range(36, 42):26 point = [landmarks.part(i).x, landmarks.part(i).y]27 left_eye_points.append(point)28 left_eye_mask = np.array(left_eye_points, dtype=np.int32)29 img_line = cv2.polylines(draw_img, [left_eye_mask], True, COLOR_WHITE, thickness=2, lineType=cv2.LINE_8)30 draw_img = cv2.fillPoly(img_line, [left_eye_mask], COLOR_WHITE, lineType=cv2.LINE_AA)31 right_eye_points = []32 for i in range(42, 48):33 point = [landmarks.part(i).x, landmarks.part(i).y]34 right_eye_points.append(point)35 right_eye_mask = np.array(right_eye_points, dtype=np.int32)36 img_line = cv2.polylines(draw_img, [right_eye_mask], True, COLOR_WHITE, thickness=2, lineType=cv2.LINE_8)37 draw_img = cv2.fillPoly(img_line, [right_eye_mask], COLOR_WHITE, lineType=cv2.LINE_AA)38 mouth_points = []39 for i in range(48, 60):40 point = [landmarks.part(i).x, landmarks.part(i).y]41 mouth_points.append(point)42 mouth_mask = np.array(mouth_points, dtype=np.int32)43 img_line = cv2.polylines(draw_img, [mouth_mask], True, COLOR_WHITE, thickness=2, lineType=cv2.LINE_8)44 draw_img = cv2.fillPoly(img_line, [mouth_mask], COLOR_WHITE, lineType=cv2.LINE_AA)45 masked_img = cv2.bitwise_and(org_img, draw_img)46 cv2.imshow('org_img', org_img)47 cv2.imshow('draw_img', draw_img)48 cv2.imshow('masked_img', masked_img)49 if cv2.waitKey(1) == ord('q'):50 sys.exit(1)51 cv2.destroyAllWindows()52# ''''''''''''''''''''' Image Code '''''''''''''''''''''''53# # org_img = cv2.imread('person.jpg')54# org_img = cv2.imread('people.jpg')55#56# if org_img is None:57# print('File Not Available')58# exit(0)59#60# org_img = cv2.resize(org_img, dsize=(img_size_width, img_size_height), interpolation=cv2.INTER_LINEAR)61# draw_img = np.zeros((org_img.shape[0], org_img.shape[1], org_img.shape[2]), np.uint8)62# gray = cv2.cvtColor(org_img, cv2.COLOR_BGR2GRAY)63# faces = detector(gray, 1)64# print("Number of Faces Detected: ", len(faces))65#66# for face in faces:67# landmarks = predictor(gray, face)68#69# left_eye_points = []70# for i in range(36, 42):71# point = [landmarks.part(i).x, landmarks.part(i).y]72# left_eye_points.append(point)73#74# left_eye_mask = np.array(left_eye_points, dtype=np.int32)75# img_line = cv2.polylines(draw_img, [left_eye_mask], True, COLOR_WHITE, thickness=2, lineType=cv2.LINE_8)76# draw_img = cv2.fillPoly(img_line, [left_eye_mask], COLOR_WHITE, lineType=cv2.LINE_AA)77#78# right_eye_points = []79# for i in range(42, 48):80# point = [landmarks.part(i).x, landmarks.part(i).y]81# right_eye_points.append(point)82#83# right_eye_mask = np.array(right_eye_points, dtype=np.int32)84# img_line = cv2.polylines(draw_img, [right_eye_mask], True, COLOR_WHITE, thickness=2, lineType=cv2.LINE_8)85# draw_img = cv2.fillPoly(img_line, [right_eye_mask], COLOR_WHITE, lineType=cv2.LINE_AA)86#87# mouth_points = []88# for i in range(48, 60):89# point = [landmarks.part(i).x, landmarks.part(i).y]90# mouth_points.append(point)91#92# mouth_mask = np.array(mouth_points, dtype=np.int32)93# img_line = cv2.polylines(draw_img, [mouth_mask], True, COLOR_WHITE, thickness=2, lineType=cv2.LINE_8)94# draw_img = cv2.fillPoly(img_line, [mouth_mask], COLOR_WHITE, lineType=cv2.LINE_AA)95#96# masked_img = cv2.bitwise_and(org_img, draw_img)97#98# save_image_file = "org_img.jpg"99# print("Save Image:", save_image_file)100# cv2.imwrite(save_image_file, org_img)101#102# save_image_file = "draw_img.jpg"103# print("Save Image:", save_image_file)104# cv2.imwrite(save_image_file, draw_img)105#106# masked_img = cv2.bitwise_and(org_img, draw_img)107# save_image_file = "masked_img.jpg"108# print("Save Image:", save_image_file)...
demo01.py
Source:demo01.py
...46 url = "http:"+url47 page_urls.append(url)48 urls.append({'title': title, 'url_list': page_urls})49 return urls50def save_image_file(url, path):51 '''52 ä¿åå¾åæ件53 '''54 ir = requests.get(url)55 if ir.status_code == 200:56 with open(path, 'wb') as f:57 f.write(ir.content)58 f.close()59def main(offset, word):60 html = get_one_page(offset, word)61 urls = parse_one_page(html)62 # å¾åæ件夹ä¸åå¨åå建63 root_path = word64 if not os.path.exists(root_path):65 os.mkdir(root_path)66 for i in range(len(urls)):67 print('---æ£å¨ä¸è½½ %s' % urls[i]['title'])68 folder = root_path + '/' + urls[i]['title']69 if not os.path.exists(folder):70 try:71 os.mkdir(folder)72 except NotADirectoryError:73 continue74 except OSError:75 continue76 url_list = urls[i]['url_list']77 for j in range(len(url_list)):78 path = folder + '/index_' + str("%02d" % j) + '.jpg'79 if not os.path.exists(path):80 save_image_file(urls[i]['url_list'][j], path)81if __name__ == '__main__':82 # æå2000个å¾éï¼åºæ¬ä¸å
å«å
¨é¨å¾é83 for i in range(100):...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!