Spaces:
Runtime error
Runtime error
test
Browse files- app.py +116 -0
- requirements.txt +4 -0
app.py
ADDED
@@ -0,0 +1,116 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import matplotlib.pyplot as plt
|
2 |
+
import numpy as np
|
3 |
+
from PIL import Image, ImageFilter
|
4 |
+
import io
|
5 |
+
import time
|
6 |
+
import os
|
7 |
+
import copy
|
8 |
+
import pickle
|
9 |
+
import datetime
|
10 |
+
import urllib.request
|
11 |
+
import gradio as gr
|
12 |
+
# from mim import install
|
13 |
+
|
14 |
+
|
15 |
+
|
16 |
+
# install('mmcv-full')
|
17 |
+
# install('mmengine')
|
18 |
+
# install('mmdet')
|
19 |
+
from mmocr.apis import MMOCRInferencer
|
20 |
+
ocr = MMOCRInferencer(det='TextSnake', rec='ABINet_Vision')
|
21 |
+
|
22 |
+
# url = (
|
23 |
+
# "https://upload.wikimedia.org/wikipedia/commons/3/38/Adorable-animal-cat-20787.jpg"
|
24 |
+
# )
|
25 |
+
# path_input = "./cat.jpg"
|
26 |
+
# urllib.request.urlretrieve(url, filename=path_input)
|
27 |
+
|
28 |
+
# url = "https://upload.wikimedia.org/wikipedia/commons/4/43/Cute_dog.jpg"
|
29 |
+
# path_input = "./dog.jpg"
|
30 |
+
# urllib.request.urlretrieve(url, filename=path_input)
|
31 |
+
|
32 |
+
# model = keras_model(weights="imagenet")
|
33 |
+
|
34 |
+
# n_steps = 50
|
35 |
+
# method = "gausslegendre"
|
36 |
+
# internal_batch_size = 50
|
37 |
+
# ig = IntegratedGradients(
|
38 |
+
# model, n_steps=n_steps, method=method, internal_batch_size=internal_batch_size
|
39 |
+
# )
|
40 |
+
|
41 |
+
|
42 |
+
# def do_process(img, baseline):
|
43 |
+
# instance = image.img_to_array(img)
|
44 |
+
# instance = np.expand_dims(instance, axis=0)
|
45 |
+
# instance = preprocess_input(instance)
|
46 |
+
# preds = model.predict(instance)
|
47 |
+
# lstPreds = decode_predictions(preds, top=3)[0]
|
48 |
+
# dctPreds = {
|
49 |
+
# lstPreds[i][1]: round(float(lstPreds[i][2]), 2) for i in range(len(lstPreds))
|
50 |
+
# }
|
51 |
+
# predictions = preds.argmax(axis=1)
|
52 |
+
# if baseline == "white":
|
53 |
+
# baselines = bls = np.ones(instance.shape).astype(instance.dtype)
|
54 |
+
# img_flt = Image.fromarray(np.uint8(np.squeeze(baselines) * 255))
|
55 |
+
# elif baseline == "black":
|
56 |
+
# baselines = bls = np.zeros(instance.shape).astype(instance.dtype)
|
57 |
+
# img_flt = Image.fromarray(np.uint8(np.squeeze(baselines) * 255))
|
58 |
+
# elif baseline == "blur":
|
59 |
+
# img_flt = img.filter(ImageFilter.GaussianBlur(5))
|
60 |
+
# baselines = image.img_to_array(img_flt)
|
61 |
+
# baselines = np.expand_dims(baselines, axis=0)
|
62 |
+
# baselines = preprocess_input(baselines)
|
63 |
+
# else:
|
64 |
+
# baselines = np.random.random_sample(instance.shape).astype(instance.dtype)
|
65 |
+
# img_flt = Image.fromarray(np.uint8(np.squeeze(baselines) * 255))
|
66 |
+
# explanation = ig.explain(instance, baselines=baselines, target=predictions)
|
67 |
+
# attrs = explanation.attributions[0]
|
68 |
+
# fig, ax = visualize_image_attr(
|
69 |
+
# attr=attrs.squeeze(),
|
70 |
+
# original_image=img,
|
71 |
+
# method="blended_heat_map",
|
72 |
+
# sign="all",
|
73 |
+
# show_colorbar=True,
|
74 |
+
# title=baseline,
|
75 |
+
# plt_fig_axis=None,
|
76 |
+
# use_pyplot=False,
|
77 |
+
# )
|
78 |
+
# fig.tight_layout()
|
79 |
+
# buf = io.BytesIO()
|
80 |
+
# fig.savefig(buf)
|
81 |
+
# buf.seek(0)
|
82 |
+
# img_res = Image.open(buf)
|
83 |
+
# return img_res, img_flt, dctPreds
|
84 |
+
|
85 |
+
|
86 |
+
# input_im = gr.inputs.Image(
|
87 |
+
# shape=(224, 224), image_mode="RGB", invert_colors=False, source="upload", type="pil"
|
88 |
+
# )
|
89 |
+
# input_drop = gr.inputs.Dropdown(
|
90 |
+
# label="Baseline (default: random)",
|
91 |
+
# choices=["random", "black", "white", "blur"],
|
92 |
+
# default="random",
|
93 |
+
# type="value",
|
94 |
+
# )
|
95 |
+
|
96 |
+
# output_img = gr.outputs.Image(label="Output of Integrated Gradients", type="pil")
|
97 |
+
# output_base = gr.outputs.Image(label="Baseline image", type="pil")
|
98 |
+
# output_label = gr.outputs.Label(label="Classification results", num_top_classes=3)
|
99 |
+
|
100 |
+
# title = "XAI - Integrated gradients"
|
101 |
+
# description = "Playground: Integrated gradients for a ResNet model trained on Imagenet dataset. Tools: Alibi, TF, Gradio."
|
102 |
+
# examples = [["./cat.jpg", "blur"], ["./dog.jpg", "random"]]
|
103 |
+
# article = "<p style='text-align: center'><a href='https://github.com/mawady' target='_blank'>By Dr. Mohamed Elawady</a></p>"
|
104 |
+
# iface = gr.Interface(
|
105 |
+
# fn=do_process,
|
106 |
+
# inputs=[input_im, input_drop],
|
107 |
+
# outputs=[output_img, output_base, output_label],
|
108 |
+
# live=False,
|
109 |
+
# interpretation=None,
|
110 |
+
# title=title,
|
111 |
+
# description=description,
|
112 |
+
# article=article,
|
113 |
+
# examples=examples,
|
114 |
+
# )
|
115 |
+
|
116 |
+
# iface.launch(debug=True)
|
requirements.txt
ADDED
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
1 |
+
pillow
|
2 |
+
mmcv>=2.0.0rc1
|
3 |
+
mmdet
|
4 |
+
mmocr
|