一:cv2.putText()

函数介绍:cv2.putText(img, str(i), (123,456), font, 2, (0,255,0), 3)
参数意思:图片,添加的文字,左上角坐标,字体,字体大小(数值越大,字体越大,可以为小数),颜色,字体粗细(越大越粗)
字体选择:FONT_HERSHEY_SIMPLEX、normal size sans-serif font、small size sans-serif font、FONT_HERSHEY_COMPLEX

二:cv2.rectangle()

函数介绍:cv2.rectangle(img,(x,y),(x+w,y+h),(0,255,0),2)
参数意思:图片,左上点坐标,右下点坐标,rgb颜色,线的宽度

三:实战例子

import cv2
import math
class ssdface():
    def __init__(self, framework='caffe', threshold=0.7):
        if framework == 'caffe':
            self.net = cv2.dnn.readNetFromCaffe('D:\pycharm\compare_three_module\SSDV2_detect_face/deploy.prototxt', 'D:/pycharm\compare_three_module/SSDV2_detect_face/res10_300x300_ssd_iter_140000_fp16.caffemodel')
        self.conf_threshold = threshold
        self.framework = framework
    def detect(self, frame):
        frameOpencvDnn = frame.copy()
        frameHeight = frameOpencvDnn.shape[0]
        frameWidth = frameOpencvDnn.shape[1]
        if self.framework == 'caffe':
            blob = cv2.dnn.blobFromImage(frameOpencvDnn, 1.0, (300, 300), [104, 117, 123], False, False)
        else:
            blob = cv2.dnn.blobFromImage(frameOpencvDnn, 1.0, (300, 300), [104, 117, 123], True, False)
        self.net.setInput(blob)
        detections = self.net.forward()
        face_rois = []
        confidence1=[]
        for i in range(detections.shape[2]):
            confidence = detections[0, 0, i, 2]
            if confidence > self.conf_threshold:
                confidence1.append(confidence)
                x1 = int(detections[0, 0, i, 3] * frameWidth)
                y1 = int(detections[0, 0, i, 4] * frameHeight)
                print(x1,y1)
                # p1=(x1,y1)
                x2 = int(detections[0, 0, i, 5] * frameWidth)
                y2 = int(detections[0, 0, i, 6] * frameHeight)
                # p2=(x2,y2)
                # cv2.rectangle(frameOpencvDnn, (x1, y1), (x2, y2), (255, 0, 0), 2)
                title = math.floor(confidence * 10 ** 2) / (10 ** 2)
                print(title)
                cv2.rectangle(frameOpencvDnn,(x1, y1), (x2, y2), (0, 0, 255), thickness=2)
                # cv2.putText(frameOpencvDnn,title, (x1,y1), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 1)
                cv2.putText(frameOpencvDnn, 'acc:' + str(title), (x1+10, y1-10),cv2.FONT_HERSHEY_SIMPLEX, 0.4, (0, 0, 255),1)
                face_rois.append(frame[y1:y2, x1:x2])
                # cv2.imshow('1',frame[y1:y2, x1:x2])
                # cv2.waitKey(0)
        return frameOpencvDnn, face_rois,confidence1
    def get_face(self, frame):
        frameOpencvDnn = frame.copy()
        frameHeight = frameOpencvDnn.shape[0]
        frameWidth = frameOpencvDnn.shape[1]
        if self.framework == 'caffe':
            blob = cv2.dnn.blobFromImage(frameOpencvDnn, 1.0, (300, 300), [104, 117, 123], False, False)
        else:
            blob = cv2.dnn.blobFromImage(frameOpencvDnn, 1.0, (300, 300), [104, 117, 123], True, False)
        self.net.setInput(blob)
        detections = self.net.forward()
        boxs, face_rois = [], []
        for i in range(detections.shape[2]):
            confidence = detections[0, 0, i, 2]
            if confidence > self.conf_threshold:
                x1 = int(detections[0, 0, i, 3] * frameWidth)
                y1 = int(detections[0, 0, i, 4] * frameHeight)
                x2 = int(detections[0, 0, i, 5] * frameWidth)
                y2 = int(detections[0, 0, i, 6] * frameHeight)
                boxs.append((x1, y1, x2, y2))
                face_rois.append(frame[y1:y2, x1:x2])
        return boxs, face_rois

if __name__ == "__main__" :
    import time
    ssdface_detect = ssdface(framework='caffe')
    imgpath = 's_l.jpg'
    save_path='D:/pycharm/compare_three_module/result/'
    srcimg = cv2.imread(imgpath)
    a = time.time()
    drawimg, face_rois,confidence = ssdface_detect.detect(srcimg)
    b = time.time()
    time = round(b - a, 3)  # 保留一位小数 2为两位
    cv2.putText(drawimg, 'time:' + str(time), (20, 40), cv2.FONT_HERSHEY_SIMPLEX, 1,(0, 0, 255))
    cv2.namedWindow('SSDV2detect', cv2.WINDOW_NORMAL)
    cv2.imshow('SSDV2detect', drawimg)
    cv2.imwrite(save_path + 'SSDV2.jpg', drawimg)
    cv2.waitKey(0)
    cv2.destroyAllWindows()

运行结果
在这里插入图片描述

GitHub 加速计划 / opencv31 / opencv
77.38 K
55.71 K
下载
OpenCV: 开源计算机视觉库
最近提交(Master分支:2 个月前 )
48668119 dnn: use dispatching for Winograd optimizations 2 天前
3dace76c flann: remove unused hdf5 header 2 天前
Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐