detectron2を遅延インストール
Browse files- app.py +10 -4
- predictor.py +2 -12
- requirements.txt +1 -1
app.py
CHANGED
|
@@ -1,3 +1,10 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import glob
|
| 2 |
import gradio as gr
|
| 3 |
from os.path import basename
|
|
@@ -6,19 +13,18 @@ from predictor import Predictor
|
|
| 6 |
|
| 7 |
predictor = Predictor()
|
| 8 |
|
| 9 |
-
def
|
| 10 |
if not model:
|
| 11 |
raise ValueError('モデルが指定されていません')
|
| 12 |
if not img_path:
|
| 13 |
return ValueError('画像が指定されていません')
|
| 14 |
-
|
| 15 |
-
return img, count
|
| 16 |
|
| 17 |
models = [basename(path) for path in glob.glob("models/*")]
|
| 18 |
images = glob.glob("test/*.jpg")
|
| 19 |
|
| 20 |
demo = gr.Interface(
|
| 21 |
-
|
| 22 |
inputs=[
|
| 23 |
gr.Dropdown(models, label="モデル", value=models[0]),
|
| 24 |
gr.Image(images[0], type='filepath', label="画像", interactive=False),
|
|
|
|
| 1 |
+
# https://huggingface.co/spaces/karolmajek/Detectron2-MaskRCNN/commit/0a02fceaca5ec92613aa2e8ce3b7d5e5043cbee5
|
| 2 |
+
try:
|
| 3 |
+
import detectron2
|
| 4 |
+
except:
|
| 5 |
+
import os
|
| 6 |
+
os.system('pip install git+https://github.com/facebookresearch/detectron2.git')
|
| 7 |
+
|
| 8 |
import glob
|
| 9 |
import gradio as gr
|
| 10 |
from os.path import basename
|
|
|
|
| 13 |
|
| 14 |
predictor = Predictor()
|
| 15 |
|
| 16 |
+
def predict(model: str, img_path: str, score_min: int):
|
| 17 |
if not model:
|
| 18 |
raise ValueError('モデルが指定されていません')
|
| 19 |
if not img_path:
|
| 20 |
return ValueError('画像が指定されていません')
|
| 21 |
+
return predictor.predict(model, img_path, score_min)
|
|
|
|
| 22 |
|
| 23 |
models = [basename(path) for path in glob.glob("models/*")]
|
| 24 |
images = glob.glob("test/*.jpg")
|
| 25 |
|
| 26 |
demo = gr.Interface(
|
| 27 |
+
predict,
|
| 28 |
inputs=[
|
| 29 |
gr.Dropdown(models, label="モデル", value=models[0]),
|
| 30 |
gr.Image(images[0], type='filepath', label="画像", interactive=False),
|
predictor.py
CHANGED
|
@@ -5,7 +5,7 @@ from detectron2.engine import DefaultPredictor
|
|
| 5 |
from detectron2.structures import Instances
|
| 6 |
from detectron2.utils.visualizer import Visualizer
|
| 7 |
from detectron2.data import MetadataCatalog
|
| 8 |
-
from detectron2.data.datasets import
|
| 9 |
|
| 10 |
DEVICE = 'cpu'
|
| 11 |
|
|
@@ -20,19 +20,9 @@ class Predictor():
|
|
| 20 |
cfg = get_cfg()
|
| 21 |
|
| 22 |
# 設定ファイルを取得
|
| 23 |
-
# cfg.merge_from_file(model_zoo.get_config_file("COCO-InstanceSegmentation/mask_rcnn_R_50_FPN_3x.yaml"))
|
| 24 |
cfg.merge_from_file(model_zoo.get_config_file("COCO-Detection/faster_rcnn_X_101_32x8d_FPN_3x.yaml"))
|
| 25 |
cfg.MODEL.DEVICE = DEVICE
|
| 26 |
-
|
| 27 |
-
# 学習済みのモデルファイルを指定
|
| 28 |
-
# cfg.MODEL.WEIGHTS = "models/pipe_seg_1.pth"
|
| 29 |
-
|
| 30 |
-
# 指定した確信度以上のパイプが検知される
|
| 31 |
-
# cfg.MODEL.ROI_HEADS.SCORE_THRESH_TEST = 0.3
|
| 32 |
-
|
| 33 |
-
# register_coco_instances("my_dataset_test", {}, "./test/_annotations.coco.json", "./test")
|
| 34 |
-
|
| 35 |
-
cfg.DATASETS.TEST = ("my_dataset_test", )
|
| 36 |
load_coco_json('./test/_annotations.coco.json', './test', 'my_dataset_test')
|
| 37 |
test_metadata = MetadataCatalog.get("my_dataset_test")
|
| 38 |
print(test_metadata)
|
|
|
|
| 5 |
from detectron2.structures import Instances
|
| 6 |
from detectron2.utils.visualizer import Visualizer
|
| 7 |
from detectron2.data import MetadataCatalog
|
| 8 |
+
from detectron2.data.datasets import load_coco_json
|
| 9 |
|
| 10 |
DEVICE = 'cpu'
|
| 11 |
|
|
|
|
| 20 |
cfg = get_cfg()
|
| 21 |
|
| 22 |
# 設定ファイルを取得
|
|
|
|
| 23 |
cfg.merge_from_file(model_zoo.get_config_file("COCO-Detection/faster_rcnn_X_101_32x8d_FPN_3x.yaml"))
|
| 24 |
cfg.MODEL.DEVICE = DEVICE
|
| 25 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
load_coco_json('./test/_annotations.coco.json', './test', 'my_dataset_test')
|
| 27 |
test_metadata = MetadataCatalog.get("my_dataset_test")
|
| 28 |
print(test_metadata)
|
requirements.txt
CHANGED
|
@@ -2,4 +2,4 @@ opencv-python-headless
|
|
| 2 |
pyyaml==5.1
|
| 3 |
torch
|
| 4 |
torchvision
|
| 5 |
-
detectron2 @ git+https://github.com/facebookresearch/detectron2.git@main
|
|
|
|
| 2 |
pyyaml==5.1
|
| 3 |
torch
|
| 4 |
torchvision
|
| 5 |
+
# detectron2 @ git+https://github.com/facebookresearch/detectron2.git@main
|