简体   繁体   English

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

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

I am starting to learn object detection in OpenCV 3.4.2 (.Net C++ 2017).我开始学习 OpenCV 3.4.2(.Net C++ 2017)中的 object 检测。 I am very interested in detecting strawberries in pictures (at the moment I am really interested in detecting just strawberries).我对检测图片中的草莓非常感兴趣(目前我真的只对检测草莓感兴趣)。 I know OpenCV has some pre-trained Haar cascade files in OpenCV directory, but there are not.xml files for strawberries (there are for body parts instead).我知道 OpenCV 在 OpenCV 目录中有一些预训练的 Haar 级联文件,但没有。xml 文件用于草莓身体部位。

So I decided to search on Google, to try to find trained strawberry Haar cascade.xml files.所以我决定在谷歌上搜索,试图找到训练有素的草莓 Haar cascade.xml 文件。 I found this.xml file XML strawberry file but I get error -49 when I try to execute the program.我找到了这个。xml 文件XML 草莓文件,但是当我尝试执行程序时出现错误 -49。 I have executed correctly the program using OpenCV files, but I cannot execute correctly when I try with GitHub XML file.我已经使用 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. 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? 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? I would like to use GitHub file.我想使用 GitHub 文件。

Edit(1) I have found this link strawberry detection in OpenCV where, if you look at the source code, it seems that the same strawberry_classifier.xml is being used.编辑(1)在 OpenCV 中找到了这个链接草莓检测,如果您查看源代码,似乎正在使用相同的草莓分类器。xml。 I don't know if the name of the file is just a coincidence (Github filename and the filename shown in the source code of the 3rd link are exactly the same).不知道文件名是不是巧合(Github文件名和第3个链接源代码中显示的文件名完全一样)。 At least it seems that the programmer (from the 3rd link) has obtained some results while using the (apparently) same.xml file that I want to use.至少看起来程序员(来自第 3 个链接)在使用我想要使用的(显然)same.xml 文件时已经获得了一些结果。 But I don't know how to use that strawberry_classifier.xml file.但我不知道如何使用那个草莓分类器.xml 文件。

Python dev here, I'm late, but in case anyone still wants to see an answer: Python dev 在这里,我迟到了,但如果有人仍然想看到答案:

The classifier from GitHub works perfectly fine as shown in this Python code (Sorry, I didn't do it in C++, but I think it won't be much different)来自 GitHub 的分类器工作得非常好,如此 Python 代码所示(对不起,我没有在 C++ 中这样做,但我认为它不会有太大不同)

The script uses your webcam as the image source.该脚本使用您的网络摄像头作为图像源。 You can show the webcam some images of strawberries, and it will recognize it:您可以向网络摄像头显示草莓的一些图像,它会识别它:

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