File size: 1,868 Bytes
ab53b8d
a092c75
4625600
1098987
b49b1bf
525d1e9
56b88d8
0a42478
1098987
0a86b8d
c0575f2
4bfcb8d
 
 
 
 
525d1e9
ab53b8d
 
 
 
834dbd4
42c5bd8
ab53b8d
e0bb95f
42c5bd8
 
e0bb95f
89addff
 
 
 
e0bb95f
4e2deeb
 
467bcef
1098987
 
 
 
84ad8ad
26ab4c7
84ad8ad
 
 
 
 
 
 
da55e58
bcd0b17
8f518ce
8de96d1
1098987
 
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
import torch
import streamlit as st
import numpy as np
from PIL import Image, ImageDraw
from transformers import pipeline
from tempfile import NamedTemporaryFile

imagepipe = pipeline("image-classification", model="flatmoon102/fruits_and_vegetables_image_classification")
detector = pipeline(model="google/owlvit-base-patch32", task="zero-shot-object-detection")

uploaded_image_file = st.file_uploader("Choose an image file")
if uploaded_image_file is not None:
    with NamedTemporaryFile() as temp:
        temp.write(uploaded_image_file.getvalue())
        temp.seek(0)
        result = imagepipe(temp.name)
        st.write(result)

st.title('Upload an image file to detection')

uploaded_image_zero_file = st.file_uploader("Choose an image file (zero)")
texts = st.text_input('tags')

if uploaded_image_zero_file is not None:
    image = Image.open(uploaded_image_zero_file)
    #temp.write(uploaded_image_file.getvalue())
    #temp.seek(0)
    outputImage = np.array(image)
    predictions = detector(
        image,
        candidate_labels=['eggs', 'apple', 'pear']
    )
    st.image(outputImage)

if st.button('apply tag'):
    tags = [['eggs', 'apple', 'pear']]
    #inputs = processor(text=tags, images=image, return_tensors="pt")
    #outputs = model(**inputs)
    #target_sizes = torch.Tensor([image.size[::-1]])
    #results = processor.post_process_object_detection(outputs=outputs, threshold=0.1, target_sizes=target_sizes)

    draw = ImageDraw.Draw(image)

    for prediction in predictions:
        box = prediction["box"]
        label = prediction["label"]
        score = prediction["score"]

        xmin, ymin, xmax, ymax = box.values()
        draw.rectangle((xmin, ymin, xmax, ymax), outline="red", width=3)
        draw.text((xmin, ymin), f"{label}: {round(score,5)}", fill="white")

    st.image(image)
    st.write(predictions)