Update app.py
Browse files
app.py
CHANGED
|
@@ -11,7 +11,6 @@ from utils import is_url, download_file, get_jpg_files, MODEL_DIR
|
|
| 11 |
|
| 12 |
TMP_DIR = "./__pycache__"
|
| 13 |
|
| 14 |
-
|
| 15 |
@dataclass
|
| 16 |
class Cfg:
|
| 17 |
detector_weights: str
|
|
@@ -21,7 +20,6 @@ class Cfg:
|
|
| 21 |
disable_faces: bool = False
|
| 22 |
draw: bool = True
|
| 23 |
|
| 24 |
-
|
| 25 |
class ValidImgDetector:
|
| 26 |
predictor = None
|
| 27 |
|
|
@@ -39,7 +37,7 @@ class ValidImgDetector:
|
|
| 39 |
mode: str,
|
| 40 |
predictor: Predictor,
|
| 41 |
) -> np.ndarray:
|
| 42 |
-
# input is
|
| 43 |
predictor.detector.detector_kwargs["conf"] = score_threshold
|
| 44 |
predictor.detector.detector_kwargs["iou"] = iou_threshold
|
| 45 |
if mode == "Use persons and faces":
|
|
@@ -79,8 +77,8 @@ def infer(photo: str):
|
|
| 79 |
photo = download_file(photo, f"{TMP_DIR}/download.jpg")
|
| 80 |
|
| 81 |
detector = ValidImgDetector()
|
| 82 |
-
if not photo or not os.path.exists(photo) or imghdr.what(photo)
|
| 83 |
-
return None, None, None, "
|
| 84 |
|
| 85 |
return detector.valid_img(photo)
|
| 86 |
|
|
@@ -88,30 +86,30 @@ def infer(photo: str):
|
|
| 88 |
if __name__ == "__main__":
|
| 89 |
with gr.Blocks() as iface:
|
| 90 |
warnings.filterwarnings("ignore")
|
| 91 |
-
with gr.Tab("
|
| 92 |
gr.Interface(
|
| 93 |
fn=infer,
|
| 94 |
-
inputs=gr.Image(label="
|
| 95 |
outputs=[
|
| 96 |
-
gr.Image(label="
|
| 97 |
-
gr.Textbox(label="
|
| 98 |
-
gr.Textbox(label="
|
| 99 |
-
gr.Textbox(label="
|
| 100 |
],
|
| 101 |
examples=get_jpg_files(f"{MODEL_DIR}/examples"),
|
| 102 |
allow_flagging="never",
|
| 103 |
cache_examples=False,
|
| 104 |
)
|
| 105 |
|
| 106 |
-
with gr.Tab("
|
| 107 |
gr.Interface(
|
| 108 |
fn=infer,
|
| 109 |
-
inputs=gr.Textbox(label="
|
| 110 |
outputs=[
|
| 111 |
-
gr.Image(label="
|
| 112 |
-
gr.Textbox(label="
|
| 113 |
-
gr.Textbox(label="
|
| 114 |
-
gr.Textbox(label="
|
| 115 |
],
|
| 116 |
allow_flagging="never",
|
| 117 |
)
|
|
|
|
| 11 |
|
| 12 |
TMP_DIR = "./__pycache__"
|
| 13 |
|
|
|
|
| 14 |
@dataclass
|
| 15 |
class Cfg:
|
| 16 |
detector_weights: str
|
|
|
|
| 20 |
disable_faces: bool = False
|
| 21 |
draw: bool = True
|
| 22 |
|
|
|
|
| 23 |
class ValidImgDetector:
|
| 24 |
predictor = None
|
| 25 |
|
|
|
|
| 37 |
mode: str,
|
| 38 |
predictor: Predictor,
|
| 39 |
) -> np.ndarray:
|
| 40 |
+
# input is RGB image, output must be RGB too
|
| 41 |
predictor.detector.detector_kwargs["conf"] = score_threshold
|
| 42 |
predictor.detector.detector_kwargs["iou"] = iou_threshold
|
| 43 |
if mode == "Use persons and faces":
|
|
|
|
| 77 |
photo = download_file(photo, f"{TMP_DIR}/download.jpg")
|
| 78 |
|
| 79 |
detector = ValidImgDetector()
|
| 80 |
+
if not photo or not os.path.exists(photo) or imghdr.what(photo) is None:
|
| 81 |
+
return None, None, None, "Please input the image correctly"
|
| 82 |
|
| 83 |
return detector.valid_img(photo)
|
| 84 |
|
|
|
|
| 86 |
if __name__ == "__main__":
|
| 87 |
with gr.Blocks() as iface:
|
| 88 |
warnings.filterwarnings("ignore")
|
| 89 |
+
with gr.Tab("Upload Mode"):
|
| 90 |
gr.Interface(
|
| 91 |
fn=infer,
|
| 92 |
+
inputs=gr.Image(label="Upload Photo", type="filepath"),
|
| 93 |
outputs=[
|
| 94 |
+
gr.Image(label="Detection Result", type="numpy"),
|
| 95 |
+
gr.Textbox(label="Has Child"),
|
| 96 |
+
gr.Textbox(label="Has Female"),
|
| 97 |
+
gr.Textbox(label="Has Male"),
|
| 98 |
],
|
| 99 |
examples=get_jpg_files(f"{MODEL_DIR}/examples"),
|
| 100 |
allow_flagging="never",
|
| 101 |
cache_examples=False,
|
| 102 |
)
|
| 103 |
|
| 104 |
+
with gr.Tab("Online Mode"):
|
| 105 |
gr.Interface(
|
| 106 |
fn=infer,
|
| 107 |
+
inputs=gr.Textbox(label="Online Picture URL"),
|
| 108 |
outputs=[
|
| 109 |
+
gr.Image(label="Detection Result", type="numpy"),
|
| 110 |
+
gr.Textbox(label="Has Child"),
|
| 111 |
+
gr.Textbox(label="Has Female"),
|
| 112 |
+
gr.Textbox(label="Has Male"),
|
| 113 |
],
|
| 114 |
allow_flagging="never",
|
| 115 |
)
|