C++_2019-06-03_机器视觉——Opencv——级联器使用

目录

STEP_1:文件夹材料准备

STEP_2:数据转换

STEP_3:数据生成vec文件 

STEP_4:数据训练

STEP_5: 模型读取检测

结果展示

STEP_6:训练经验


STEP_1:文件夹材料准备

# 文件
root/
    neg/**.bmp             负样本
    pos/**.bmp             正样本
    opencv_createsamples   正样本转换
    opencv_traincascade    正负样本训练

STEP_2:数据转换

# 数据设置{尺寸、名字、写入文件txt、目标ROI位置替换}

## 批量【 改变大小 】
```
for x in *.jpg; do convert -resize 256x256\! $x $x; done
```
## 批量【 改变名字 】
```
rename "s/^/image_/" x.bmp
```
## 批量【 改变名字 】
```
i=0;for x in *; do mv $x $i.bmp; let i=i+1; done
```
## 批量【 写名字名到文件 】
```
ls ./neg/*.* >neg.txt
ls ./pos/*.* >pos.txt
```

## 修改 pos.txt >>> xx.bmp 1 x y w h <<<

## 完成样本到文件

STEP_3:数据生成vec文件 

-num 7092      正样本数量的大小

-w 40 -h 60      按目标的大小进行规划

## 修改样本截取大小
sudo ./opencv_createsamples \
-info pos.txt \
-bg neg.txt \
-num 7092 \
-vec pos.vec \
-w 40 -h 60

STEP_4:数据训练

手动建文件夹model,-data model 

-w 40 -h 60 必须和上面规划的尺寸一致

-featureType LBP 有两种模型可选:LBP 和 HAAR

## 创建 model 文件夹、样本数量、尺寸、LBP 或者 HAAR、占用内存、准确率、错误率、使用的模型
sudo ./opencv_traincascade \
-data model \
-vec pos.vec -bg neg.txt \
-numPos 1000 -numNeg 1000 \
-numStages 20 \
-w 40 -h 60 \
-featureType LBP \
-precalcValBufSize 12400 -precalcIdxBufSize 12400 \
-minHitRate 0.9999  -maxFalseAlarmRate 0.25 \
-mode ALL

STEP_5:模型读取检测

python objectdetection.py                                 打开相机进行检测
python objectdetection.py --image = xx.jpg     读取图片进行检测
python objectdetection.py --video = xx.mp4    读取视频进行检测

# -*- coding: utf-8 -*-
import cv2
import argparse
import sys
import numpy as np

parser = argparse.ArgumentParser(description='Object Detection')
parser.add_argument('--image', help='Path to image file.')
parser.add_argument('--video', help='Path to video file.')
args = parser.parse_args()

# Process inputs
winName = 'Object detection in OpenCV-DJH'
cv2.namedWindow(winName, cv2.WINDOW_NORMAL)
outputFile = "out.avi"
if (args.image):
    # Open the image file
    if not os.path.isfile(args.image):
        print("Input image file ", args.image, " doesn't exist")
        sys.exit(1)
    cap = cv2.VideoCapture(args.image)
    outputFile = args.image[:-4]+'out.jpg'
elif (args.video):
    # Open the video file
    if not os.path.isfile(args.video):
        print("Input video file ", args.video, " doesn't exist")
        sys.exit(1)
    cap = cv2.VideoCapture(args.video)
    outputFile = args.video[:-4]+'out.avi'
else:
    # Webcam input
    cap = cv2.VideoCapture(0)

# Get the video writer initialized to save the output video
if (not args.image):
    vid_writer = cv2.VideoWriter(outputFile, \
        cv2.VideoWriter_fourcc('M','J','P','G'), 30, \
        (round(cap.get(cv2.CAP_PROP_FRAME_WIDTH)),\
            round(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))))



if __name__ == '__main__':   
    ## >>>>> 其他 <<<<<< ##
    Cascade = cv2.CascadeClassifier(r'./model/cascade.xml') 
    
    while cv2.waitKey(1) < 0:
        
        # get frame from the video
        hasFrame, frame = cap.read()
        
        # Stop the program if reached end of video
        if not hasFrame:
            print("Done processing !!!")
            cv2.waitKey(3000)
            cap.release()
            break
        gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
        
        objs = Cascade.detectMultiScale(gray,
                                        scaleFactor=1.15,
                                        minNeighbors=5,
                                        minSize=(5,5),
                                        flags = cv2.CASCADE_SCALE_IMAGE)
        
        for (x,y,w,h) in objs:
            frame = cv2.rectangle(frame, (x,y), (x+w,y+h), (255,0,0),2)
            

        # Write the frame with the detection boxes
        if (args.image):
            cv2.imwrite(outputFile, frame.astype(np.uint8))
        else:
            vid_writer.write(frame.astype(np.uint8))

        cv2.imshow(winName, frame)

结果展示

检测效果还不错,有车的模型、人脸的模型

STEP_6:训练经验

1. w h 按实际物体比例

2. storage: 看样本数量,多则多,少则少

3. acceptanceRatio: 在0.0004(4×10^3)左右寻找最佳的模型

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

智能之心

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值