File size: 4,101 Bytes
4bf5062
7f92cc3
2d6cef6
aa730c9
 
 
2899d04
8692633
aa730c9
 
 
2899d04
aa730c9
f8a17d5
 
ceeebb8
f8a17d5
1a130e9
ce7faf8
 
f8a17d5
4bf5062
6e6c277
 
77e9de4
6e6c277
aa730c9
 
72ae1a0
1af867d
e1b7276
2899d04
149bdd2
72ae1a0
7f92cc3
f8a17d5
 
7f92cc3
72ae1a0
77e9de4
c95acae
77e9de4
73a8242
 
72ae1a0
3262d9a
72ae1a0
 
 
 
 
 
 
 
149bdd2
72ae1a0
 
7f92cc3
 
 
b65bfeb
 
72ae1a0
 
 
 
 
 
 
 
7f92cc3
 
 
df9e0ac
7f92cc3
e1b7276
2899d04
e1b7276
72ae1a0
c5d6f4f
73a8242
72ae1a0
aa730c9
e1b7276
 
 
d85b158
df9e0ac
2899d04
 
 
e16b4e8
2899d04
 
 
 
 
ed79734
2899d04
 
d85b158
 
ea4f5ec
4a2d00d
5811184
 
4a2d00d
78c504f
d85b158
8692633
d85b158
a11bd4c
d85b158
4954064
ea4f5ec
80af6ab
 
 
 
 
 
ea4f5ec
80af6ab
ea4f5ec
80af6ab
7040a84
8692633
4a2d00d
 
2899d04
e1b7276
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import os
import cv2
import glob
import torch
import streamlit as st
import numpy as np
import base64
from random import randrange
from PIL import Image, ImageDraw
from transformers import pipeline
from tempfile import NamedTemporaryFile
from st_clickable_images import clickable_images

def clearEphimeral():
    for filename in glob.glob('/tmp/cropped_image_*'):
        os.remove(filename)


st.set_page_config(layout="wide")

clearEphimeral()

if 'wip' not in st.session_state:
    st.session_state['wip'] = False
    st.session_state['image'] = None

detector = pipeline("object-detection", model="sergioska/yolo-finetuned-flyers")

col1, col2, col3 = st.columns([0.5, 0.3, 0.2])

gibsOk = False
totalGibs = 0

with col1:

    clearEphimeral()

    st.header('flyer')
    uploaded_image_zero_file = st.file_uploader("Choose an image file (zero)")

    if not st.session_state['wip']:
        imageLocation = st.empty()
    else:
        imageLocation = st.image(st.session_state['image'])
    
    if uploaded_image_zero_file is not None:
        image = Image.open(uploaded_image_zero_file)
        outputImage = np.array(image)
        predictions = detector(
            image,
            candidate_labels=['product']
        )
        imageLocation.image(outputImage)
    
    if st.button('detect') or st.session_state['wip']:
    
        draw = ImageDraw.Draw(image)

        arrImg = np.array(image)
        imageCv = cv2.cvtColor(arrImg, cv2.COLOR_RGB2BGR)

        i = 0
        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=5)
            draw.text((xmin, ymin), f"{label}: {round(score,5)}", fill="white")

            crop_img = imageCv[ymin:ymax, xmin:xmax]
            if crop_img.size > 0:
                cv2.imwrite("/tmp/cropped_image_{}.jpg".format(i), crop_img)
                i += 1

        totalGibs = i
        gibsOk = True
        imageLocation.image(image)
        st.session_state['wip'] = True
        st.session_state['image'] = image
        st.write(predictions)
        
with col2:
    st.header('gibs')
    images = []
    imagesForOpen = []
    for cropped in glob.glob('/tmp/cropped_image_*'):
        with open(cropped, "rb") as image:
            encoded = base64.b64encode(image.read()).decode()
            images.append(f"data:image/jpeg;base64,{encoded}")
            imagesForOpen.append(cropped)
    clicked = clickable_images(
        images,
        titles=[f"Image #{str(i)}" for i in range(totalGibs)],
        div_style={"display": "flex", "justify-content": "center", "flex-wrap": "wrap"},
        img_style={"margin": "5px", "height": "150px"},
        key="cropped_images"
    )

with col3:
    st.header('details')
    colors = ["red", "yellow", "purple", "blue", "green"]
    if gibsOk:
        if clicked == -1:
            clicked = 0
        detectorForContents = pipeline("object-detection", model="sergioska/yolo-finetuned-flyers-content")
        imageForDetails = Image.open(imagesForOpen[clicked])
        outputImageForDetails = np.array(imageForDetails)
        predictionsForDetails = detectorForContents(
            imageForDetails,
            candidate_labels=['title', 'price']
        )
        drawForDetails = ImageDraw.Draw(imageForDetails)
        c = 0
        for predictionsForDetail in predictionsForDetails:
            box = predictionsForDetail["box"]
            label = predictionsForDetail["label"]
            score = predictionsForDetail["score"]
    
            xmin, ymin, xmax, ymax = box.values()
            drawForDetails.rectangle((xmin, ymin, xmax, ymax), outline=colors[c], width=5)
            drawForDetails.text((xmin, ymin), f"{label}: {round(score,5)}", fill="white")
            c += 1

        st.image(imageForDetails)
        st.write(predictionsForDetails)
        

    st.markdown(f"Image #{clicked} clicked" if clicked > -1 else "No image clicked")