File size: 2,189 Bytes
74428c3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
65
66
67
68
69
70
71
72
import gradio as gr
from PIL import Image
import time,os
from pathlib import Path 
base_dir = '猫咪表情包'
selected_dir = 'selected'
files = [str(x) for x in 
         Path(base_dir).rglob('*.jp*g') 
         if 'checkpoint' not in str(x)]
files_idx=0
selected_count=0
    
def show_img(path):
    return Image.open(path)
def fn_before(done,todo):
    global files_idx
    if files_idx>0:
        files_idx-=1
    path=files[files_idx]
    img=show_img(path)
    return done,todo,path,img
def fn_next(done,todo):
    global files_idx
    if files_idx<len(files)-1:
        files_idx+=1
    path=files[files_idx]
    img=show_img(path)
    return done,todo,path,img
def save_selected(img_path,done,todo):
    global msg,selected_count
    if img_path not in msg:
        msg=msg+'  '+img_path
        selected_count+=1
        done=selected_count
        todo=len(files)-selected_count
    return msg,done,todo 
def get_default_msg():
    global msg
    msg=''
    return msg
    
    
with gr.Blocks() as demo:
    with gr.Row():
        total = gr.Number(len(files),label='总数量')
        with gr.Row(scale = 1):
            bn_before = gr.Button("上一张")
            bn_next = gr.Button("下一张")
        with gr.Row(scale = 2):
            done = gr.Number(value=selected_count,label='已完成')
            todo = gr.Number(value=len(files)-selected_count,label='待完成')
    with gr.Row():
        with gr.Column():
            path = gr.Text(files[0],lines=1, label='当前图片路径')
            feedback_button = gr.Button("选择图片",variant="primary")
            msg = gr.TextArea(value=get_default_msg,lines=3,max_lines = 5)
        with gr.Column():
            img = gr.Image(value = show_img(files[0]),type='pil')
    
    bn_before.click(fn_before,
                 inputs= [done,todo], 
                 outputs=[done,todo,path,img])
    bn_next.click(fn_next,
                 inputs= [done,todo], 
                 outputs=[done,todo,path,img])
    feedback_button.click(save_selected,
                         inputs = [path,done,todo],
                         outputs =[msg,done,todo]
                         )
gr.close_all()
demo.launch()