-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
64 lines (52 loc) · 2.22 KB
/
app.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
# from turtle import width
from google.cloud import vision
from PIL import Image, ImageDraw
import argparse
import streamlit as st
import os
def load_image(image_file):
img = Image.open(image_file)
return img
def detect_face(face_file, max_results=4):
client = vision.ImageAnnotatorClient()
content = face_file.read()
image = vision.Image(content=content)
return client.face_detection(
image=image, max_results=max_results).face_annotations
def highlight_faces(image, faces, output_filename):
im = Image.open(image)
draw = ImageDraw.Draw(im)
# Sepecify the font-family and the font-size
for face in faces:
box = [(vertex.x, vertex.y)
for vertex in face.bounding_poly.vertices]
draw.line(box + [box[0]], width=5, fill='#00ff00')
# Place the confidence value/score of the detected faces above the
# detection box in the output image
draw.text(((face.bounding_poly.vertices)[0].x,
(face.bounding_poly.vertices)[0].y - 30),
str(format(face.detection_confidence, '.3f')) + '%',
fill='#FF0000')
im.save(output_filename)
def main(input_filename, output_filename, max_results):
with open(input_filename, 'rb') as image:
faces = detect_face(image, max_results)
print('Found {} face{}'.format(
len(faces), '' if len(faces) == 1 else 's'))
print('Writing to file {}'.format(output_filename))
# Reset the file pointer, so we can read the file again
image.seek(0)
highlight_faces(image, faces, output_filename)
if __name__ == '__main__':
st.subheader("Image")
image_file = st.file_uploader("Upload image", type=["png","jpg","jpeg"])
input_dir = "/app/images/"
output_dir = input_dir+"detected/"
output_file = output_dir+"output.jpg"
if image_file is not None:
img = load_image(image_file)
st.image(img,width=400,caption="The original image")
with open(os.path.join(input_dir,image_file.name),"wb") as f:
f.write(image_file.getbuffer())
main(input_dir+image_file.name, output_file, 15)
st.image(load_image(output_file),width=400,caption="Cloud Vision AI's detected image")