Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,77 +1,77 @@
|
|
1 |
import os
|
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 |
from fastsam import FastSAM, FastSAMPrompt
|
77 |
os.system('wget https://huggingface.co/spaces/An-619/FastSAM/resolve/main/weights/FastSAM.pt')
|
@@ -109,11 +109,10 @@ def greet(img):
|
|
109 |
lns = read_license_number(img)
|
110 |
if len(lns):
|
111 |
seg = segment_solar_panel(img)
|
112 |
-
return (seg,
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
# + "็ๆ
๏ผ" + check_solarplant_broken(img))
|
117 |
return (img, "็ฉบๅฐใใใ")
|
118 |
|
119 |
iface = gr.Interface(fn=greet, inputs="image", outputs=["image", "text"])
|
|
|
1 |
import os
|
2 |
+
import yolov5
|
3 |
+
|
4 |
+
# load model
|
5 |
+
model = yolov5.load('keremberke/yolov5m-license-plate')
|
6 |
+
|
7 |
+
# set model parameters
|
8 |
+
model.conf = 0.5 # NMS confidence threshold
|
9 |
+
model.iou = 0.25 # NMS IoU threshold
|
10 |
+
model.agnostic = False # NMS class-agnostic
|
11 |
+
model.multi_label = False # NMS multiple labels per box
|
12 |
+
model.max_det = 1000 # maximum number of detections per image
|
13 |
+
|
14 |
+
# set image
|
15 |
+
def license_plate_detect(img):
|
16 |
+
# perform inference
|
17 |
+
results = model(img, size=640)
|
18 |
|
19 |
+
# inference with test time augmentation
|
20 |
+
results = model(img, augment=True)
|
21 |
|
22 |
+
# parse results
|
23 |
+
if len(results.pred):
|
24 |
+
predictions = results.pred[0]
|
25 |
+
boxes = predictions[:, :4] # x1, y1, x2, y2
|
26 |
+
scores = predictions[:, 4]
|
27 |
+
categories = predictions[:, 5]
|
28 |
+
return boxes
|
29 |
+
|
30 |
+
from PIL import Image
|
31 |
+
# image = Image.open(img)
|
32 |
+
import pytesseract
|
33 |
+
|
34 |
+
def read_license_number(img):
|
35 |
+
boxes = license_plate_detect(img)
|
36 |
+
if boxes:
|
37 |
+
return [pytesseract.image_to_string(
|
38 |
+
image.crop(bbox.tolist()))
|
39 |
+
for bbox in boxes]
|
40 |
+
|
41 |
+
from transformers import CLIPProcessor, CLIPModel
|
42 |
+
vit_model = CLIPModel.from_pretrained("openai/clip-vit-base-patch32")
|
43 |
+
processor = CLIPProcessor.from_pretrained("openai/clip-vit-base-patch32")
|
44 |
+
|
45 |
+
def zero_shot_classification(image, labels):
|
46 |
+
inputs = processor(text=labels,
|
47 |
+
images=image,
|
48 |
+
return_tensors="pt",
|
49 |
+
padding=True)
|
50 |
+
outputs = vit_model(**inputs)
|
51 |
+
logits_per_image = outputs.logits_per_image # this is the image-text similarity score
|
52 |
+
return logits_per_image.softmax(dim=1) # we can take the softmax to get the label probabilities
|
53 |
+
|
54 |
+
installed_list = []
|
55 |
+
# image = Image.open(requests.get(url, stream=True).raw)
|
56 |
+
def check_solarplant_installed_by_license(license_number_list):
|
57 |
+
if len(installed_list):
|
58 |
+
return [license_number in installed_list
|
59 |
+
for license_number in license_number_list]
|
60 |
+
|
61 |
+
def check_solarplant_installed_by_image(image, output_label=False):
|
62 |
+
zero_shot_class_labels = ["bus with solar panel grids",
|
63 |
+
"bus without solar panel grids"]
|
64 |
+
probs = zero_shot_classification(image, zero_shot_class_labels)
|
65 |
+
if output_label:
|
66 |
+
return zero_shot_class_labels[probs.argmax().item()]
|
67 |
+
return probs.argmax().item() == 0
|
68 |
+
|
69 |
+
def check_solarplant_broken(image):
|
70 |
+
zero_shot_class_labels = ["white broken solar panel",
|
71 |
+
"normal black solar panel grids"]
|
72 |
+
probs = zero_shot_classification(image, zero_shot_class_labels)
|
73 |
+
idx = probs.argmax().item()
|
74 |
+
return zero_shot_class_labels[idx].split(" ")[1-idx]
|
75 |
|
76 |
from fastsam import FastSAM, FastSAMPrompt
|
77 |
os.system('wget https://huggingface.co/spaces/An-619/FastSAM/resolve/main/weights/FastSAM.pt')
|
|
|
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"])
|