????????
? ? ?斷斷續續地做圖像識別的應用,使用過各種圖像識別算法,一開始使用openCV 做教室學生計數的程序。以后又使用YOLO 做醫學傷口檢測程序。最近,開始使用meta 公司的Detectron2.打算做OCR 文檔結構分析
? ? ?Detectron2 的開發者是 Meta 的 Facebook AI 研究 (FAIR) 團隊,他們表示“我們開發 Detectron2 的目標是支持當今各種尖端的物體檢測和分割模型,同時也服務于不斷變化的尖端研究領域。”
? ? ? ?Detectron2 是一個基于 Pytorch 框架構建的深度學習模型,據稱該框架是目前最有前途的模塊化目標檢測庫之一。
本文記錄在MAC Mini M4 上做的測試。
安裝
pip install 'git+https://github.com/facebookresearch/detectron2.git@v0.4#egg=detectron2'
pip install layoutparser
pip install Pillow==9.5.0
代碼
#https://towardsdatascience.com/understanding-detectron2-demo-bc648ea569e5/
import argparseimport cv2
import numpy as np
import refrom detectron2 import model_zoo
from detectron2.config import get_cfg, CfgNode
from detectron2.data import MetadataCatalog
from detectron2.engine import DefaultPredictor
from detectron2.structures import Instances
from detectron2.utils.visualizer import Visualizer, VisImagedef _get_parsed_args() -> argparse.Namespace:"""Create an argument parser and parse arguments.:return: parsed arguments as a Namespace object"""parser = argparse.ArgumentParser(description="Detectron2 demo")# default model is the one with the 2nd highest mask AP# (Average Precision) and very high speed from Detectron2 model zooparser.add_argument("--base_model",default="COCO-InstanceSegmentation/mask_rcnn_R_101_FPN_3x.yaml",help="Base model to be used for training. This is most often ""appropriate link to Detectron2 model zoo.")parser.add_argument("--images",nargs="+",help="A list of space separated image files that will be processed. ""Results will be saved next to the original images with ""'_processed_' appended to file name.")return parser.parse_args()if __name__ == "__main__":args: argparse.Namespace = _get_parsed_args()cfg: CfgNode = get_cfg()cfg.merge_from_file(model_zoo.get_config_file(args.base_model))cfg.MODEL.ROI_HEADS.SCORE_THRESH_TEST = 0.4cfg.MODEL.WEIGHTS = model_zoo.get_checkpoint_url(args.base_model)cfg.MODEL.DEVICE = "mps"predictor: DefaultPredictor = DefaultPredictor(cfg)image_file: strfor image_file in args.images:img: np.ndarray = cv2.imread(image_file)output: Instances = predictor(img)["instances"]v = Visualizer(img[:, :, ::-1],MetadataCatalog.get(cfg.DATASETS.TRAIN[0]),scale=1.0)result: VisImage = v.draw_instance_predictions(output.to("cpu"))result_image: np.ndarray = result.get_image()[:, :, ::-1]# get file name without extension, -1 to remove "." at the endout_file_name: str = re.search(r"(.*)\.", image_file).group(0)[:-1]out_file_name += "_processed.png"cv2.imwrite(out_file_name, result_image)
注意:在這個過程中出現錯誤:
raise AssertionError("Torch not compiled with CUDA enabled")AssertionError: Torch not compiled with CUDA enabled
Mac Mini 的GPU 稱為mps。我添加了 cfg.MODEL.DEVICE = "mps"。你可以測試一下:
import torch
print(torch.mps.is_available())
True
運行?
python detectron2_demo4.py --images david-clarke-KTF-gr3uWvs-unsplash.jpg
輸入的圖片?
輸出
輸出的速度比較慢,大約121秒。
另一個圖片識別
姑娘與狗
耗費時間99秒。
先這樣吧,日后慢慢地學習。?