Spaces:
Runtime error
Runtime error
Raja
commited on
Commit
·
cba8c08
1
Parent(s):
dffff3f
Testing whole program it 1
Browse files
app.py
CHANGED
@@ -4,13 +4,86 @@ import streamlit as st
|
|
4 |
from datasets import load_dataset
|
5 |
from PIL import Image
|
6 |
from matplotlib.pyplot import imshow
|
|
|
|
|
7 |
|
8 |
def load_dataset_from_Hugging_Face():
|
9 |
reloaded_dataset_thermal = load_dataset("gokulraja17/rice-thermal-demo")
|
10 |
reloaded_dataset_rgb = load_dataset("gokulraja17/rice-rgb-demo2")
|
11 |
return reloaded_dataset_thermal,reloaded_dataset_rgb
|
12 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
13 |
dataset_thermal,dataset_rgb = load_dataset_from_Hugging_Face()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
14 |
uploaded_file = st.file_uploader("Choose a image file", type=["jpg","png"])
|
15 |
|
16 |
if uploaded_file is not None:
|
|
|
4 |
from datasets import load_dataset
|
5 |
from PIL import Image
|
6 |
from matplotlib.pyplot import imshow
|
7 |
+
from transformers import pipeline
|
8 |
+
from transformers import AutoFeatureExtractor, AutoModelForImageClassification
|
9 |
|
10 |
def load_dataset_from_Hugging_Face():
|
11 |
reloaded_dataset_thermal = load_dataset("gokulraja17/rice-thermal-demo")
|
12 |
reloaded_dataset_rgb = load_dataset("gokulraja17/rice-rgb-demo2")
|
13 |
return reloaded_dataset_thermal,reloaded_dataset_rgb
|
14 |
|
15 |
+
def load_model_from_Hugging_Face():
|
16 |
+
feature_extractor_thermal = AutoFeatureExtractor.from_pretrained("gokulraja17/convnext-fine-tuned-thermal-demo2")
|
17 |
+
model_thermal = AutoModelForImageClassification.from_pretrained("gokulraja17/convnext-fine-tuned-thermal-demo2")
|
18 |
+
feature_extractor_rgb = AutoFeatureExtractor.from_pretrained("gokulraja17/convnext-fine-tuned-rgb-demo2")
|
19 |
+
model_rgb = AutoModelForImageClassification.from_pretrained("gokulraja17/convnext-fine-tuned-rgb-demo2")
|
20 |
+
return feature_extractor_thermal,model_thermal,feature_extractor_rgb,model_rgb
|
21 |
+
|
22 |
+
def get_image_and_label(reloaded_dataset_thermal,reloaded_dataset_rgb,index):
|
23 |
+
url_thermal = reloaded_dataset_thermal["test"][index]
|
24 |
+
url_rgb = reloaded_dataset_rgb["test"][index]
|
25 |
+
return url_thermal["image"],url_thermal["label"],url_rgb["image"],url_rgb["label"]
|
26 |
+
|
27 |
+
def create_pipeline(feature_extractor_thermal,model_thermal,feature_extractor_rgb,model_rgb):
|
28 |
+
|
29 |
+
pipe_rgb = pipeline("image-classification",
|
30 |
+
model=model_rgb,
|
31 |
+
feature_extractor=feature_extractor_rgb)
|
32 |
+
pipe_thermal = pipeline("image-classification",
|
33 |
+
model=model_thermal,
|
34 |
+
feature_extractor=feature_extractor_thermal)
|
35 |
+
return pipe_thermal,pipe_rgb
|
36 |
+
|
37 |
+
def get_output_label(result):
|
38 |
+
max = -999
|
39 |
+
label = ""
|
40 |
+
for i in result:
|
41 |
+
if(max<i['score']):
|
42 |
+
max = i['score']
|
43 |
+
label = i["label"]
|
44 |
+
return label
|
45 |
+
|
46 |
dataset_thermal,dataset_rgb = load_dataset_from_Hugging_Face()
|
47 |
+
labels=dataset_thermal["train"].features["label"].names
|
48 |
+
id2label={k:v for k,v in enumerate(labels)}
|
49 |
+
print(id2label)
|
50 |
+
label2id = {v:k for k,v in enumerate(labels)}
|
51 |
+
print(label2id)
|
52 |
+
|
53 |
+
feature_extractor_thermal,model_thermal,feature_extractor_rgb,model_rgb = load_model_from_Hugging_Face()
|
54 |
+
index=3
|
55 |
+
image_thermal,thermal_label,image_rgb,rgb_label = get_image_and_label(dataset_thermal,dataset_rgb,index)
|
56 |
+
print("Thermal Input...")
|
57 |
+
imshow(np.asarray(image_thermal))
|
58 |
+
print(id2label[thermal_label])
|
59 |
+
print("RGB Input...")
|
60 |
+
imshow(np.asarray(image_rgb))
|
61 |
+
print(id2label[rgb_label])
|
62 |
+
|
63 |
+
def consolidate_score(thermal_result,rgb_result):
|
64 |
+
consol_score_1=[]
|
65 |
+
consol_score_2=[]
|
66 |
+
consol_score=[]
|
67 |
+
for i in range(0,len(thermal_result)):
|
68 |
+
consol_score_1.append(list(thermal_result[i].values()))
|
69 |
+
consol_score_2.append(list(rgb_result[i].values()))
|
70 |
+
temp_label=""
|
71 |
+
for i in range(0,len(thermal_result)):
|
72 |
+
temp_label=consol_score_1[i][1]
|
73 |
+
for j in range(0,len(rgb_result)):
|
74 |
+
if(temp_label==consol_score_2[j][1]):
|
75 |
+
consol_score.append([temp_label,consol_score_1[i][0],consol_score_2[j][0]])
|
76 |
+
output_consol=[]
|
77 |
+
for i in consol_score:
|
78 |
+
output_consol.append({'score':(((i[2]/100)*35)+((i[1]/100)*65)),'label':i[0]})
|
79 |
+
return output_consol
|
80 |
+
|
81 |
+
pipe_thermal,pipe_rgb = create_pipeline(feature_extractor_thermal,model_thermal,feature_extractor_rgb,model_rgb)
|
82 |
+
thermal_result=pipe_thermal(image_thermal)
|
83 |
+
rgb_result=pipe_rgb(image_rgb)
|
84 |
+
consolidated_result=get_output_label(consolidate_score(thermal_result,rgb_result))
|
85 |
+
print(consolidated_result)
|
86 |
+
|
87 |
uploaded_file = st.file_uploader("Choose a image file", type=["jpg","png"])
|
88 |
|
89 |
if uploaded_file is not None:
|