#coding:utf-8
# Object Detection using SSD Inception arquitecture trained on COCO dataset
import cv2
import sys

FROZEN_GRAPH = "datas/models/tensorflow/ssd_inception_v2_coco.pb"
PB_TXT = "datas/models/tensorflow/ssd_inception_v2_coco.pbtxt"
SIZE = 300

from utils.coco_labels import LABEL_MAP

def run(img):
    cvNet = cv2.dnn.readNetFromTensorflow(FROZEN_GRAPH, PB_TXT)

    rows = img.shape[0]
    cols = img.shape[1]
    cvNet.setInput(cv2.dnn.blobFromImage(img, 1.0/127.5, (SIZE, SIZE), (127.5, 127.5, 127.5), swapRB=True, crop=False))
    cvOut = cvNet.forward()

    for detection in cvOut[0,0,:,:]:
        score = float(detection[2])
        if score > 0.3:
            left = detection[3] * cols
            top = detection[4] * rows
            right = detection[5] * cols
            bottom = detection[6] * rows
            
            cv2.rectangle(img, (int(left), int(top)), (int(right), int(bottom)), (23, 230, 210), thickness=2)
            cv2.putText(img, LABEL_MAP[int(detection[1])], (int(left), int(top)), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0))


    return img


if __name__ == "__main__":
    image = cv2.imread("datas/images/bird.jpg")
    image = run(image)
    cv2.imshow("Image", image)
    cv2.waitKey(0)

Opencv3.4调用SSD tensorflow模型进行物体检测

相关文章:

  • 2021-08-03
  • 2021-07-02
  • 2021-12-04
  • 2021-09-20
  • 2021-10-19
  • 2022-01-17
  • 2021-12-25
  • 2021-08-17
猜你喜欢
  • 2021-04-14
  • 2021-10-10
  • 2021-06-03
  • 2021-06-09
  • 2021-06-10
  • 2022-12-23
  • 2021-11-23
相关资源
相似解决方案