Nanopc T4 使用OpenCV

识别长方形: 

import cv2
import cv2 as cv
import time
import platform
import os

# 获取操作系统类型
os_type = platform.system()
if os_type == "Windows":
    # Windows系统
    cap = cv.VideoCapture(0)  # 使用第零个摄像头
elif os_type == "Linux":
    # Linux系统
    cap = cv.VideoCapture(10)  # 使用第十个摄像头
    if not cap.isOpened():
        print("Cannot capture from camera. Exiting.")
        os._exit(1)  # 退出程序
last_time = time.time()




while (True):
    ret, frame = cap.read()
    imgContour = frame.copy()

    imgCanny = cv2.Canny(frame, 60, 60)  # Canny算子边缘检测
    contours, hierarchy = cv2.findContours(imgCanny, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)  # 寻找轮廓点
    for obj in contours:
        area = cv2.contourArea(obj)  # 计算轮廓内区域的面积
        # cv2.drawContours(imgContour, obj, -1, (255, 0, 0), 4)  # 绘制轮廓线
        perimeter = cv2.arcLength(obj, True)  # 计算轮廓周长
        approx = cv2.approxPolyDP(obj, 0.02 * perimeter, True)  # 获取轮廓角点坐标
        CornerNum = len(approx)  # 轮廓角点的数量
        x, y, w, h = cv2.boundingRect(approx)  # 获取坐标值和宽度、高度

        if CornerNum == 4:
            if 90 < w != h > 50:
                objType = "ChangFangXing"
                cv2.rectangle(imgContour, (x, y), (x + w, y + h), (0, 0, 255), 2)  # 绘制边界框
                cv2.putText(imgContour, objType, (x + (w // 2), y + (h // 2)), cv2.FONT_HERSHEY_COMPLEX, 0.6, (0, 0, 0),
                            1)  # 绘制文字

    cv2.imshow("shape Detection", imgContour)

    if cv.waitKey(1) & 0xFF == ord('q'):
        break

cap.release()
cv.destroyAllWindows()

识别人脸1:

import cv2
import cv2 as cv
import time
import platform
import os

# 获取操作系统类型
os_type = platform.system()
if os_type == "Windows":
    # Windows系统
    cap = cv.VideoCapture(0)  # 使用第零个摄像头
elif os_type == "Linux":
    # Linux系统
    cap = cv.VideoCapture(10)  # 使用第十个摄像头
    if not cap.isOpened():
        print("Cannot capture from camera. Exiting.")
        os._exit(1)  # 退出程序
last_time = time.time()

img = cv.imread("D:\WorkSpace\Python\qsc.png")


def template_matching(img_match, img, arithmetic_model):
    '''
     【作用】
      进行图片模板匹配
     【参数1】
      模板图片
     【参数2】
      进行匹配的图片
     【参数3】
      算法模型
     【返回】
      无
    '''

    # 进行模板匹配
    result = cv.matchTemplate(img, img_match, arithmetic_model)

    # 获取最小最大匹配值,还有对应的坐标
    min_value, max_value, min_coordinate, max_coordinate = cv.minMaxLoc(result)

    # 默认最佳最大值,当算法为CV_TM_SQDIFF或CV_TM_SQDIFF_NORMED时改为最小值
    best_coordinate = max_coordinate
    if arithmetic_model == cv.TM_SQDIFF or arithmetic_model == cv.TM_SQDIFF_NORMED:
        best_coordinate = min_coordinate

    # 获取匹配图片的高和宽
    m_height, m_width = img_match.shape[:2]

    # 矩形的起始点和结束点
    r_start = best_coordinate
    r_end = (best_coordinate[0] + m_width, best_coordinate[1] + m_height);

    # 矩形的颜色和线的宽度
    r_color = (0, 100, 40)
    r_line_width = 2

    # 绘制矩形并展示
    cv.rectangle(img, r_start, r_end, r_color, r_line_width)
    cv.imshow("Qu ShiChao", img)


while (True):
    ret, frame = cap.read()

    template_matching(img, frame, cv.TM_SQDIFF)

    if cv.waitKey(1) & 0xFF == ord('q'):
        break

cap.release()
cv.destroyAllWindows()

通模型识别人脸

import cv2
import cv2 as cv
import time
import platform
import os

# 获取操作系统类型
os_type = platform.system()
if os_type == "Windows":
    # Windows系统
    cap = cv.VideoCapture(0)  # 使用第零个摄像头
elif os_type == "Linux":
    # Linux系统
    cap = cv.VideoCapture(10)  # 使用第十个摄像头
    if not cap.isOpened():
        print("Cannot capture from camera. Exiting.")
        os._exit(1)  # 退出程序
last_time = time.time()

while (True):
    ret, frame = cap.read()

    # 这里是你的xml存放路径
    face_cascade = cv2.CascadeClassifier("D:\WorkSpace\Python\lbpcascade_frontalface.xml")
    # 开始人脸检测
    faces = face_cascade.detectMultiScale(frame, scaleFactor=1.03, minNeighbors=6)
    # 先复制一张图片
    frame1 = frame.copy()
    # 在检测到的人脸中操作
    for x, y, w, h in faces:
        # 画出人脸框
        frame1 = cv2.rectangle(frame1, (x, y), (x + w, y + h), (0, 255, 0), 2)
        # 找出人脸区域
        face_area = frame1[y:y + h, x:x + w]
    # 在人脸区域检测人眼

    cv2.imshow('face', frame1)


    if cv.waitKey(1) & 0xFF == ord('q'):
        break

cap.release()
cv.destroyAllWindows()



相关推荐

  1. Nanopc T4 使用OpenCV

    2024-05-10 16:48:03       12 阅读
  2. opencv(4)

    2024-05-10 16:48:03       14 阅读
  3. OpenCV 4.X 使用CvxText在图片显示汉字

    2024-05-10 16:48:03       8 阅读
  4. Opencc4j 开源中文繁简体使用介绍

    2024-05-10 16:48:03       27 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-05-10 16:48:03       16 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-05-10 16:48:03       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-05-10 16:48:03       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-05-10 16:48:03       18 阅读

热门阅读

  1. RabbitMQ

    RabbitMQ

    2024-05-10 16:48:03      10 阅读
  2. OceanBase 中的ROWID与Oracle的差异与如何迁移

    2024-05-10 16:48:03       13 阅读
  3. 面试前的刷题,要有充分的准备

    2024-05-10 16:48:03       7 阅读
  4. 【C++ list所有函数举例如何使用】

    2024-05-10 16:48:03       11 阅读
  5. 【AAGNet】GNN模型用于BREP数模分割代码复现笔记

    2024-05-10 16:48:03       12 阅读
  6. 将每个Excel文件的数据量统一减少至120000行

    2024-05-10 16:48:03       13 阅读
  7. 商城数据库88张表DDL(71-88)

    2024-05-10 16:48:03       11 阅读
  8. 典型相关分析模型评价的标准和代码

    2024-05-10 16:48:03       9 阅读