繁体   English   中英

OPENCV - 如何使用 Haar 级联 Github xml 文件在 Z5BD4C87976F348E6A5E949?D

[英]OPENCV - How to use Haar cascade Github xml files in OpenCV?

我开始学习 OpenCV 3.4.2(.Net C++ 2017)中的 object 检测。 我对检测图片中的草莓非常感兴趣(目前我真的只对检测草莓感兴趣)。 我知道 OpenCV 在 OpenCV 目录中有一些预训练的 Haar 级联文件,但没有。xml 文件用于草莓身体部位。

所以我决定在谷歌上搜索,试图找到训练有素的草莓 Haar cascade.xml 文件。 我找到了这个。xml 文件XML 草莓文件,但是当我尝试执行程序时出现错误 -49。 我已经使用 OpenCV 文件正确执行了程序,但是当我尝试使用 GitHub XML 文件时无法正确执行。

I have found this thread here on StackOverflow StackOverFlow thread about GitHub XML files in OpenCV and a user claims that it's not possible to use GitHub XML files into OpenCV.

My question is about if there is a way to use the XML GitHub file I have posted in this thread in OpenCV or I need to train my own XML file? 我想使用 GitHub 文件。

编辑(1)在 OpenCV 中找到了这个链接草莓检测,如果您查看源代码,似乎正在使用相同的草莓分类器。xml。 不知道文件名是不是巧合(Github文件名和第3个链接源代码中显示的文件名完全一样)。 至少看起来程序员(来自第 3 个链接)在使用我想要使用的(显然)same.xml 文件时已经获得了一些结果。 但我不知道如何使用那个草莓分类器.xml 文件。

Python dev 在这里,我迟到了,但如果有人仍然想看到答案:

来自 GitHub 的分类器工作得非常好,如此 Python 代码所示(对不起,我没有在 C++ 中这样做,但我认为它不会有太大不同)

该脚本使用您的网络摄像头作为图像源。 您可以向网络摄像头显示草莓的一些图像,它会识别它:

import cv2 #import library

#define Haar Cascade Classifier
Strawberry_Classifier = cv2.CascadeClassifier(r"C:\Users\Strawberry.xml")
VideoCapture = cv2.VideoCapture(0) #capture video from camera

#set video size
VideoCapture.set(3, 540)
VideoCapture.set(4, 360)

while True:
     #Connect video and convert
     Connection_Success, Video = VideoCapture.read()                              #returns a bool and video array in one tuple (sucess, video array)
     RGB_video = cv2.cvtColor(Video, cv2.COLOR_BGR2RGB)                           #converts to suitable format
     Detect_Strawberry = Strawberry_Classifier.detectMultiScale(RGB_video, 1.3, 13) #MODIFY THIS FOR LESS/MORE DETECTION ACCURACY 

     #Detect Strawberry
     for(x,y,w,h) in Detect_Strawberry: #x,y width, height
          cv2.rectangle(Video, (x, y), (x + w, y + h), (0, 255, 0), 3)                         #Put a rectangle around Strawberry
          
     cv2.imshow("Window", Video) #show video

     #quit if q is pressed, quit
     QuitKey = cv2.waitKey(30)
     if QuitKey == ord("q"):
          VideoCapture.release()
          cv2.destroyAllWindows()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM