File size: 2,372 Bytes
e9e5323
c1115fd
c9ce858
 
 
 
97b86b5
2ccd650
94239c6
 
 
 
 
 
 
 
 
 
97b86b5
c1115fd
 
 
 
 
 
 
 
c9ce858
 
c1115fd
 
 
 
 
c9ce858
 
c1115fd
97b86b5
 
c9ce858
 
 
e46d69b
 
 
 
 
 
 
97b86b5
e46d69b
c1115fd
c9ce858
 
e46d69b
 
 
 
 
 
 
c9ce858
 
e46d69b
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
import os
import torch
import urllib
from PIL import Image
import streamlit as st
from pathlib import Path


def check_suffix(file='yolov5s.pt', suffix=('.pt',), msg=''):
    # Check file(s) for acceptable suffix
    if file and suffix:
        if isinstance(suffix, str):
            suffix = [suffix]
        for f in file if isinstance(file, (list, tuple)) else [file]:
            s = Path(f).suffix.lower()  # file suffix
            if len(s):
                assert s in suffix, f"{msg}{f} acceptable suffix is {suffix}"


def check_file(file, suffix=''):
    # Search/download file (if necessary) and return path
    check_suffix(file, suffix)  # optional
    file = str(file)  # convert to str()
    if os.path.isfile(file) or not file:  # exists
        return file
    elif file.startswith(('http:/', 'https:/')):  # download
        url = file  # warning: Pathlib turns :// -> :/
        # '%2F' to '/', split https://url.com/file.txt?auth
        file = Path(urllib.parse.unquote(file).split('?')[0]).name
        if os.path.isfile(file):
            print(f'Found {url} locally at {file}')  # file already exists
        else:
            print(f'Downloading {url} to {file}...')
            torch.hub.download_url_to_file(url, file)
            assert Path(file).exists() and Path(file).stat(
            ).st_size > 0, f'File download failed: {url}'  # check
        return file


st.title("Hololive Waifu Classification")

image = st.text_input('Image URL', '')
imgsz = st.text_input('Image Size', 1280)
conf = st.text_input('Confidence threshold', 0.25)
iou = st.text_input('IoU threshold', 0.45)
multi_label = st.selectbox('Multiple labels per box', (False, True))
agnostic = st.selectbox('Class-agnostic', (False, True))
amp = st.selectbox('Automatic Mixed Precision inference', (False, True))
max_det  = st.text_input('Maximum number of detections per image', 1000)

if st.button('Excute'):
    image = check_file(image)
    input_image = Image.open(image)
    model = torch.hub.load('ultralytics/yolov5', 'custom', path='best.pt')
    model.conf = float(conf)
    model.max_det = int(max_det)
    model.iou = float(iou)
    model.agnostic = agnostic
    model.multi_label = multi_label
    model.amp = amp 
    results = model(input_image, size=int(imgsz))
    for img in results.render():
        st.image(img)
    st.write(results.pandas().xyxy[0])