Spaces:
Runtime error
Runtime error
Add chart
Browse files- Processor.py +2 -2
- app.py +22 -5
Processor.py
CHANGED
@@ -6,12 +6,12 @@ from typing import List
|
|
6 |
|
7 |
class Processor():
|
8 |
def __init__(self, learn: Learner):
|
9 |
-
self.
|
10 |
self.__model = torch.hub.load(
|
11 |
'ultralytics/yolov5', 'yolov5x6', trust_repo=True)
|
12 |
|
13 |
def classify_image(self, images: NDArray) -> str:
|
14 |
-
return self.
|
15 |
|
16 |
def filter_image(self, image: Image) -> bool:
|
17 |
results = self.__model(image)
|
|
|
6 |
|
7 |
class Processor():
|
8 |
def __init__(self, learn: Learner):
|
9 |
+
self.inference = learn
|
10 |
self.__model = torch.hub.load(
|
11 |
'ultralytics/yolov5', 'yolov5x6', trust_repo=True)
|
12 |
|
13 |
def classify_image(self, images: NDArray) -> str:
|
14 |
+
return self.inference.predict(images)
|
15 |
|
16 |
def filter_image(self, image: Image) -> bool:
|
17 |
results = self.__model(image)
|
app.py
CHANGED
@@ -15,21 +15,33 @@ def initialize_app():
|
|
15 |
def process_images(images, processor: Processor):
|
16 |
filtered_images = []
|
17 |
result = []
|
|
|
|
|
18 |
|
19 |
for image in images:
|
20 |
image = Image.open(image)
|
21 |
if processor.filter_image(image):
|
22 |
filtered_images.append(np.asarray(image))
|
23 |
-
|
24 |
for img in filtered_images:
|
25 |
result.append(processor.classify_image(img)[0])
|
26 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
27 |
outfit = mode(result)
|
28 |
|
29 |
with open(f'./texts/{outfit}.txt') as text:
|
30 |
personality = text.read()
|
31 |
|
32 |
-
return {
|
|
|
33 |
|
34 |
|
35 |
# Streamlit UI
|
@@ -38,7 +50,7 @@ processor = initialize_app()
|
|
38 |
|
39 |
st.title('Instagram Clothes Psychology (Photos)')
|
40 |
uploaded_photos = st.file_uploader(label="Upload photos", type=[
|
41 |
-
'jpg', 'jpeg'
|
42 |
|
43 |
photos_empty = True if len(uploaded_photos) == 0 else False
|
44 |
|
@@ -48,5 +60,10 @@ is_clicked = st.button(label='Predict Personality',
|
|
48 |
if is_clicked:
|
49 |
with st.spinner('Please wait...'):
|
50 |
result = process_images(uploaded_photos, processor)
|
51 |
-
|
52 |
-
|
|
|
|
|
|
|
|
|
|
|
|
15 |
def process_images(images, processor: Processor):
|
16 |
filtered_images = []
|
17 |
result = []
|
18 |
+
class_names = list(
|
19 |
+
map(lambda name: {name: 0}, processor.inference.dls.vocab))
|
20 |
|
21 |
for image in images:
|
22 |
image = Image.open(image)
|
23 |
if processor.filter_image(image):
|
24 |
filtered_images.append(np.asarray(image))
|
25 |
+
|
26 |
for img in filtered_images:
|
27 |
result.append(processor.classify_image(img)[0])
|
28 |
|
29 |
+
if len(result) == 0:
|
30 |
+
return None
|
31 |
+
|
32 |
+
for res_name in result:
|
33 |
+
for idx, class_name in enumerate(class_names):
|
34 |
+
for key, value in class_name.items():
|
35 |
+
if res_name == key:
|
36 |
+
class_names[idx][key] = value + 1
|
37 |
+
|
38 |
outfit = mode(result)
|
39 |
|
40 |
with open(f'./texts/{outfit}.txt') as text:
|
41 |
personality = text.read()
|
42 |
|
43 |
+
return {'outfit': outfit.title(), 'personality': personality,
|
44 |
+
'chart': class_names}
|
45 |
|
46 |
|
47 |
# Streamlit UI
|
|
|
50 |
|
51 |
st.title('Instagram Clothes Psychology (Photos)')
|
52 |
uploaded_photos = st.file_uploader(label="Upload photos", type=[
|
53 |
+
'jpg', 'jpeg'], accept_multiple_files=True)
|
54 |
|
55 |
photos_empty = True if len(uploaded_photos) == 0 else False
|
56 |
|
|
|
60 |
if is_clicked:
|
61 |
with st.spinner('Please wait...'):
|
62 |
result = process_images(uploaded_photos, processor)
|
63 |
+
if result is None:
|
64 |
+
st.write('Tidak ditemukan gambar yang valid')
|
65 |
+
else:
|
66 |
+
st.header('Your personality is..')
|
67 |
+
st.subheader(result['outfit'])
|
68 |
+
st.text(result['personality'])
|
69 |
+
st.bar_chart(result['chart'])
|