fudii0921's picture
Update app.py
f30d5fa verified
import gradio as gr
import cv2
import base64
def satsuei_show():
return gr.update(visible=True)
# 写真撮影機能
def capture_image(image,username,userid, format):
if (username != "" or userid != ""):
image_array = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
filename = ""
if (format == "PNG"):
filename = userid + ".png"
scale_x, scale_y = 0.3, 0.3 # 幅と高さの倍率
resized_image = cv2.resize(image_array, None, fx=scale_x, fy=scale_y, interpolation=cv2.INTER_LINEAR)
#new_width, new_height = 354, 472
#resized_image = cv2.resize(image_array, (new_width, new_height), interpolation=cv2.INTER_LINEAR)
cv2.imwrite(filename, resized_image, [int(cv2.IMWRITE_PNG_COMPRESSION), 9])
else:
filename = userid + ".jpg"
scale_x, scale_y = 0.3, 0.3 # 幅と高さの倍率
resized_image = cv2.resize(image_array, None, fx=scale_x, fy=scale_y, interpolation=cv2.INTER_LINEAR)
#new_width, new_height = 354, 472
#resized_image = cv2.resize(image_array, (new_width, new_height), interpolation=cv2.INTER_LINEAR)
cv2.imwrite(filename, resized_image, [int(cv2.IMWRITE_JPEG_QUALITY), 90])
# base64 encode
with open(filename, "rb") as image_file:
b64 = base64.b64encode(image_file.read()).decode('utf-8')
sqlcmd = "insert into insight_face_recognition (userid, username, pre_embedding) values ('"+userid+"','"+username+"','"+b64+"')"
return image,sqlcmd
else:
gr.Warning("ユーザー情報が正しくありません!")
return None,"No Statement"
with gr.Blocks(css="footer {visibility: hidden;} #custom_button {width: 200px; margin: 0 auto; background-color: #031861; color: black;}", theme=gr.themes.Glass(), title="顔写真を撮影") as live:
with gr.Row():
username = gr.Textbox(label="ユーザー名",info="ex) リチャード・ホウ")
userid = gr.Textbox(label="ユーザーID",info="ex) richard.huh")
with gr.Row():
format = gr.Dropdown( ["PNG", "JPEG"], label="画像フォーマット", info="画像フォーマットを選択します" )
webcam = gr.Image(sources=["webcam"], streaming=False, label="撮影画像")
output_image = gr.Image(label="撮影画像")
with gr.Row():
take_btn = gr.Button("撮影",elem_id="custom_button",visible=False)
with gr.Row():
inscmd = gr.Textbox(label="SQL",show_copy_button=True)
userid.change(fn=satsuei_show, inputs=None, outputs=[take_btn])
take_btn.click(fn=capture_image, inputs=[webcam, username, userid, format], outputs=[output_image, inscmd])
live.launch(favicon_path='favicon.ico')