Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -39,3 +39,81 @@ def read_license_number(img):
|
|
39 |
return [pytesseract.image_to_string(
|
40 |
image.crop(bbox.tolist()))
|
41 |
for bbox in boxes]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
39 |
return [pytesseract.image_to_string(
|
40 |
image.crop(bbox.tolist()))
|
41 |
for bbox in boxes]
|
42 |
+
|
43 |
+
from transformers import CLIPProcessor, CLIPModel
|
44 |
+
model = CLIPModel.from_pretrained("openai/clip-vit-base-patch32")
|
45 |
+
processor = CLIPProcessor.from_pretrained("openai/clip-vit-base-patch32")
|
46 |
+
|
47 |
+
def zero_shot_classification(image, labels):
|
48 |
+
inputs = processor(text=labels,
|
49 |
+
images=image,
|
50 |
+
return_tensors="pt",
|
51 |
+
padding=True)
|
52 |
+
outputs = model(**inputs)
|
53 |
+
logits_per_image = outputs.logits_per_image # this is the image-text similarity score
|
54 |
+
return logits_per_image.softmax(dim=1) # we can take the softmax to get the label probabilities
|
55 |
+
|
56 |
+
installed_list = []
|
57 |
+
# image = Image.open(requests.get(url, stream=True).raw)
|
58 |
+
def check_solarplant_installed_by_license(license_number_list):
|
59 |
+
if len(installed_list):
|
60 |
+
return [license_number in installed_list
|
61 |
+
for license_number in license_number_list]
|
62 |
+
|
63 |
+
def check_solarplant_installed_by_image(image, output_label=False):
|
64 |
+
zero_shot_class_labels = ["bus with solar panel grids",
|
65 |
+
"bus without solar panel grids"]
|
66 |
+
probs = zero_shot_classification(image, zero_shot_class_labels)
|
67 |
+
if output_label:
|
68 |
+
return zero_shot_class_labels[probs.argmax().item()]
|
69 |
+
return probs.argmax().item() == 0
|
70 |
+
|
71 |
+
def check_solarplant_broken(image):
|
72 |
+
zero_shot_class_labels = ["white broken solar panel",
|
73 |
+
"normal black solar panel grids"]
|
74 |
+
probs = zero_shot_classification(image, zero_shot_class_labels)
|
75 |
+
idx = probs.argmax().item()
|
76 |
+
return zero_shot_class_labels[idx][1-idx]
|
77 |
+
|
78 |
+
from fastsam import FastSAM, FastSAMPrompt
|
79 |
+
|
80 |
+
model = FastSAM('./FastSAM.pt')
|
81 |
+
DEVICE = 'cpu'
|
82 |
+
IMAGE_PATH = 'sam.jpg'
|
83 |
+
def segment_solar_panel(img):
|
84 |
+
img.Save(IMAGE_PATH)
|
85 |
+
everything_results = model(IMAGE_PATH, device=DEVICE, retina_masks=True, imgsz=1024, conf=0.4, iou=0.9,)
|
86 |
+
prompt_process = FastSAMPrompt(IMAGE_PATH, everything_results, device=DEVICE)
|
87 |
+
|
88 |
+
# everything prompt
|
89 |
+
ann = prompt_process.everything_prompt()
|
90 |
+
|
91 |
+
# bbox default shape [0,0,0,0] -> [x1,y1,x2,y2]
|
92 |
+
ann = prompt_process.box_prompt(bbox=[[200, 200, 300, 300]])
|
93 |
+
|
94 |
+
# text prompt
|
95 |
+
ann = prompt_process.text_prompt(text='solar panel grids')
|
96 |
+
|
97 |
+
# point prompt
|
98 |
+
# points default [[0,0]] [[x1,y1],[x2,y2]]
|
99 |
+
# point_label default [0] [1,0] 0:background, 1:foreground
|
100 |
+
ann = prompt_process.point_prompt(points=[[620, 360]], pointlabel=[1])
|
101 |
+
|
102 |
+
prompt_process.plot(annotations=ann,output_path='./dog.jpg',)
|
103 |
+
return Image.Open('./dog.jpg')
|
104 |
+
|
105 |
+
|
106 |
+
import gradio as gr
|
107 |
+
|
108 |
+
def greet(img):
|
109 |
+
lns = read_license_number(img)
|
110 |
+
if len(lns):
|
111 |
+
seg = segment_solar_panel(img)
|
112 |
+
return (seg,
|
113 |
+
"車牌: " + '; '.join(lns) + "\n\n" \
|
114 |
+
+ "類型: "+ check_solarplant_installed_by_image(img, True) + "\n\n" \
|
115 |
+
+ "狀態:" + check_solarplant_broken(img))
|
116 |
+
return (img, "空地。。。")
|
117 |
+
|
118 |
+
iface = gr.Interface(fn=greet, inputs="image", outputs=["image", "text"])
|
119 |
+
iface.launch()
|