Kaggle competition: ASN10e Final Submission - Detect COML Faces
pop.rename(columns={'Country or Area':'Country'}, inplace=True )<define_variables>
!pip install face_recognition
map_state = {'US':'United States',
'Korea, South':'South Korea',
'Cabo Verde' : 'Carpe Verde',
'Congo(Brazzaville)': 'Republic of the Congo',
'Cote d'Ivoire':'Ivory Coast',
'Czechia':'Czech Republic',
'Eswatini':'Swaziland',
'Holy See':'Vatican City',
'North Macedonia':'Macedonia',
'Taiwan*':'Taiwan',
'occupied Palestinian territory':'Palestine'
}
map_state_rev = {v: k for k, v in map_state.items() }<feature_engineering>
path = '.. /input/comlfaces/'
known_face_encodings = []
known_face_names = []
for i in os.listdir(path):
try:
known_face_encodings.append(face_recognition.face_encodings(face_recognition.load_image_file(path+i)) [0])
 known_face_names.append(i.split('_')[0].capitalize() + " " + i.split('_')[1].capitalize())
 print('added ' + i)
 except:
print('FAILED ' + i)
 print('
Done!' )
pop['country'] = pop['country'].apply(lambda x: map_state_rev[x] if x in map_state_rev else x)
<merge>
filename = []
person_id = []
uid = []
xmin = []
xmax = []
ymin = []
ymax = []
path = ".. /input/asn10e-final-submission-coml-facial-recognition"
os.chdir(path)
 image_set = ["0.jpg", "1.jpg"]
mapping = pd.read_csv('person_id_mapping.csv')
 for image in image_set:
unknown_image = face_recognition.load_image_file(image)
 face_locations = face_recognition.face_locations(unknown_image)
 face_encodings = face_recognition.face_encodings(unknown_image, face_locations)
 pil_image = Image.fromarray(unknown_image)
 draw = ImageDraw.Draw(pil_image)
 for(top, right, bottom, left), face_encoding in zip(face_locations, face_encodings):
filename.append(image)
 matches = face_recognition.compare_faces(known_face_encodings, face_encoding)
 name = "Unknown"
face_distances = face_recognition.face_distance(known_face_encodings, face_encoding)
 best_match_index = np.argmin(face_distances)
 if matches[best_match_index]:
name = known_face_names[best_match_index]
draw.rectangle(((left, top),(right, bottom)) , outline=(0, 0, 255))
text_width, text_height = draw.textsize(name)
 draw.rectangle(((left, bottom - text_height - 10),(right, bottom)) , fill=(0, 0, 255), outline=(0, 0, 255))
draw.text(( left + 6, bottom - text_height - 5), name, fill=(255, 255, 255, 255))
if(name == "Sakshum Kulsrestha"):
name = "Sakshum Kulshrestha"
if name == "Joshua Lo":
name = "Rung-Chuan Lo"
pid = mapping[mapping["Person"]==name]["person_id"]
pid = pid.index.values[0]
person_id.append(pid)
 image_id = image.split(".")[0] + "_" + str(pid)
 uid.append(image_id)
 xmax.append(float(right))
xmin.append(float(left))
ymin.append(float(bottom))
ymax.append(float(top))
del draw
display(pil_image)
 os.chdir(".. /.. /working")
 data = {'filename':filename, 'person_id':person_id, 'id':uid, 'xmin':xmin, 'xmax':xmax, 'ymin':ymin, 'ymax':ymax}
df = pd.DataFrame(data)
 df.loc[37,"person_id"] = 16
df.loc[37,"id"] = "0_16"
df.to_csv('submission.csv', index=False )