File size: 2,801 Bytes
e6b441b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c3a4405
c70f24d
 
 
1c37d2b
e6b441b
 
c257461
c70f24d
 
 
1c37d2b
e6b441b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f30d5fa
e6b441b
 
 
 
 
 
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
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')