I'm in my OPENCV_ROOT folder and running the following command in Win-x64:
opencv_createsamples -bgcolor 0 -bgthresh 0 -maxxangle 1.1 -maxyangle 1.1
maxzangle 0.5 -maxidev 40 -w 80 -h 40
-img ABSOLUTE_PATH_TO_POSITIVE_IMAGES_FOLDER\car.jpg
-bg ABSOLUTE_PATH_TO_PROJECT\negatives.txt
-vec ABSOLUTE_PATH_TO_SAMPLES_FOLDER\car.jpg.vec
-num 125
My negatives.txt looks like this:
negative_images\city01_02.jpg
negative_images\city01_04.jpg
negative_images\city01_05.jpg
negative_images\city01_06.jpg
negative_images\city01_07.jpg
This is my output:
Info file name: (NULL)
Img file name: ABSOLUTE_PATH_TO_POSITIVE_IMAGES_FOLDER\car.jpg
Vec file name: ABSOLUTE_PATH_TO_SAMPLES_FOLDER\car.jpg.vec
BG file name: ABSOLUTE_PATH_TO_PROJECT\negatives.txt
Num: 125
BG color: 0
BG threshold: 0
Invert: FALSE
Max intensity deviation: 40
Max x angle: 1.1
Max y angle: 1.1
Max z angle: 0.5
Show samples: FALSE
Width: 80
Height: 40
Create training samples from single image applying distortions...
Invalid background description file.
What's invalid about my negatives collection file (negatives.txt)? I tried listing the files within it both with absolute paths and with relative paths. I'm following this tutorial.
If you created the file on Windows, and running it on Linux e.g. Ubuntu, you have to change the "End of line sequence".
Click on the bottom of the editor on CR LF and change it to LF and try again!
Notepad++:
Visual Studio Code:
Geany: Documents > Set Line Endings > Convert and Set to LF (Unix)
Problem solved! The issue was simply that I had written the collection file names in Notepad++, and it all seemed fine. But when I happened to open the same file in notepad, I noticed there were no newlines after each image filename! When I added the newlines, and ran the command, the samples got created without errors!
I had the same problem. Just skip lines between addresses of the images. For example: before my negatives.dat file was:
./Negative_Images/197.
./Negative_Images/69.
./Negative_Images/510.
./Negative_Images/513.
./Negative_Images/169.png
Now it's:
./Negative_Images/197.png
./Negative_Images/69.png
./Negative_Images/510.png
./Negative_Images/513.png
./Negative_Images/169.png
Problem solved.
In my case, I was creating this bg.txt file with a script. Basically, it was doing :
for (int i = 0; i < numberOfImages; i++)
{
myDescFile << imagesFolder + std::to_string(i) + ".jpg\n";
}
I simply added another \n and the problem went away!
myDescFile << imagesFolder + std::to_string(i) + ".jpg\n\n";
EDIT:
You also have to make sure that your paths have/or\\ as separators. Else, you will be able to run the opencv_createsamples.exe cmd, won't have error, but will never see the Done at the end.
ex:L:/imgTraining/plant/bnw/0.jpg
Here's a picture of a good bg file from notepad++ (With View>Show Symbol>Show End of Line activated and Edit>EOL Conversion>Windows (CR LF) selected):
Install dos2unix on Linux by sudo apt-get install dos2unix.
And now you just need to use dos2unix YourFileName.txt.
Related
Here's what shows when loading the live stream demo for Yolov4:
Webcam index: 2
[ WARN:0] global ../modules/videoio/src/cap_gstreamer.cpp (935) open OpenCV | GStreamer warning: Cannot query video position: status=0, value=-1, duration=-1
Video stream: 2304 x 1536
Objects:
Then it starts finding objects with 2 fps.
How do I change the video stream resolution to 1080p or 720p? The frame rate is very slow and this appears to be the fix.
Can't find it within the makefile or cfg folder. Any thoughts? Is this an opencv problem?
Thanks!
cfg settings:
[net]
batch=64
subdivisions=8
# Training
#width=512
#height=512
width=320
height=320
channels=3
momentum=0.949
decay=0.0005
angle=0
saturation = 1.5
exposure = 1.5
hue=.1
learning_rate=0.0013
burn_in=1000
max_batches = 500500
policy=steps
steps=400000,450000
scales=.1,.1
I tried with the built-in camera and connected my phone(IP) and got 1080 on both with smooth results. I didn't find anywhere to change the webcam settings which are stuck on 2304x1536. Where would camera settings be located?
After searching around for a solution to this issue myself I finally found it!
In the darknet/src/ folder is a file named "image_opencv.cpp". At lines 597 and 598 you will find the following 2 commented commands:
//cap->set(CV_CAP_PROP_FRAME_WIDTH, 1280);
&
//cap->set(CV_CAP_PROP_FRAME_HEIGHT, 960);
After trying out these commands a lot more errors showed up, this is due to yolov4 (and my install) using OpenCV 4.1.1. Which has a different syntax. Your resolution should change to 1920x1080 if you replace the two aforementioned commands with these:
cap->set(cv::CAP_PROP_FRAME_WIDTH, 1920);
cap->set(cv::CAP_PROP_FRAME_HEIGHT, 1080);
Notice that the comment slashes have been removed as to activate the commands.
I am trying to create a vec file from my positive image samples. I'm using the latest version of Opencv. The command I am using is this:
opencv_createsamples -info Positive.info -num 3750 -w 24 -h 24 -vec Positive.vec
All my images are bigger than 24x24, so I don't believe that that is the issue. I've tried searching online, but I don't find anyone with this error while doing the createsamples command. The output that I get when I run that is this:
Info file name: Positive.info
Img file name: (NULL)
Vec file name: Positive.vec
BG file name: (NULL)
Num: 3750
BG color: 0
BG threshold: 80
Invert: FALSE
Max intensity deviation: 40
Max x angle: 1.1
Max y angle: 1.1
Max z angle: 0.5
Show samples: FALSE
Width: 24
Height: 24
Max Scale: -1
RNG Seed: 12345
Create training samples from images collection...
OpenCV: terminate handler is called! The last OpenCV error is:
OpenCV(4.0.0-alpha) Error: Assertion failed (0 <= roi.x && 0 <= roi.width && roi.x + roi.width <= m.cols && 0 <= roi.y && 0 <= roi.height && roi.y + roi.height <= m.rows) in cv::Mat::Mat, file c:\build\master_winpack-build-win64-vc15\opencv\modules\core\src\matrix.cpp, line 465
This is my first time using opencv, so I'm very unclear as to what this error is. Anyone have any experience with this or can point me in the right direction to correcting the error?
This is Priyanka.
I was also getting the same error and I was trying to get resolution to it somehow.
But I could not find anything on internet.
But I am able to solve the problem successfully.
2-3 things that were missing as below:
I used opencv_annotation.exe to generate the -info (text file). This utility embeds complete image path into the file, where as the opencv_createsamples.exe expects that the images are present in the same folder as of the -info file. So what used to happen was the path of the image was getting concatenated with the path of the -info file.
i.e Suppose -info file is available at D:\MyPath\DataSet\annotations.txt and the images are at D:\MyPath\DataSet\Vehicles\*.png. In this case, while executing the opencv_createsamples.exe, the complete image path becomes D:\MyPath\DataSet\D:\MyPath\DataSet\Vehicles\image000.png, which the utility is not able to find.
So cater to the correct path kindly take care of below things
put the -info file in the same path as of images.
ensure that the -info file does not have the path for any image. It should contain only the
<image name without complete path> <object instances> <x y, width height>
With this change the image path was taken correctly and my issue got resolved.
Try doing this. It may help you.
This is Tushar Pathade, this error comes when your info.txt(file which contains information of objects to be detected) file contains some wrong information like image_name 1 0 0 0 0 or image_name 2 10 20 30 40. In first examples coordinates are (0,0,0,0) which is impossible while in second example no. of objects are 2 but coordinates of only one objects are mentioned.
So this lines produced this error. So first remove it and then run. You will not get any error then.
As priyanka mentioned above, this also need to take care of first but this will produced another error. It is nothing but image not found error. So to avoid it put info.txt file in a folder where negative images are. Basically u will see path in error so accordingly handle it.
Thank you.
I got the same error.
The error is about the bounding boxes, sometimes while creating bounding boxes you create a box outside the image pixel so that region is not get accepted and then you get this error.
Solution :
I used divide and conquer tech for this solution. What I did is I've created a new text file and in that text file I pasted some files from the original info or text file I ran the code if its creating samples then good if not then delete any one image from that text file run again*(run the create_samples again)*. do this until you get the clean file. I know this is too tedious task but this works.!
Thanks
I face the same issue now, if the num argument is less than 950 it works for me.
I'm studying opencv and dlib, for a face detector to use on a university project, and I'm really new at this whole thing of machine learning and computer vision. How can I use the evaluation code from FDDB to evaluate my code for face detection? I'm using dlib's CNN method for detecting faces from images.
import cv2
import dlib
image = cv2.imread('..\\pessoas\\beatles.jpg')
detector = dlib.cnn_face_detection_model_v1("..\\mmods\\mmod_human_face_detector.dat")
detectedFaces = detector(image)
for face in detectedFaces:
l, t, r, b, c = (int(face.rect.left()), int(face.rect.top()), int(face.rect.right()), int(face.rect.bottom()),
face.confidence)
cv2.rectangle(image, (l, t), (r, b), (255, 0, 0), 2)
cv2.imshow("CNN Detector", image)
cv2.waitKey(0)
cv2.destroyAllWindows()
As you can see the code is pretty simple, but I have to calculate precision, recall, and F1-score to plot the ROC curves and I'm don't know yet how to do it, the readme on the project's github doesn't help.
As to me in ubuntu16, I have to done it by the following steps:
Download the fddb original images dataset which you detect face and get detection result.You can download it here.Here is my directory:
Join all the images file path to a txt file , and join all the fddb annotations to a txt file.
You can download all the files here
As to me I move all the FDDB-FOLD-%d.txt to the directory all_file_path, and then join them to one file by cat * > filePath.txt
Join all the FDDB-fold-%d-ellipseList.txt to one txt by cat *ellipse*.txt > annotFile.txt
Note you may no need to create it, because runEvaluate.pl have do it for you during the running process.
3.Create FDDB evalute exe, download the source code here here
And then compile it , you may change the makefile, see the reason here, add
INCS = -I/usr/local/include/opencv
LIBS = -L/usr/local/lib -lopencv_core -lopencv_imgproc -lopencv_highgui
-lopencv_ml -lopencv_video -lopencv_features2d -lopencv_calib3d
-lopencv_objdetect -lopencv_contrib -lopencv_legacy
to the make file.
Evaluate, you can use the runEvaluate.pl to evaluate it , but as to me(ubuntu16), I can’t run it directly.
4.1 change the GUNPLOT path(you should install gnuplot first using it to create ROC image )
4.2 I use rectangle detection model, so I change $detFormat to 0.
my $detFormat = 0; # 0: rectangle, 1: ellipse 2: pixels
4.3 All the images relative path:
my $listFile ="/home/xy/face_sample/evaluation/compareROC/FDDB-folds/filePath.txt";
4.4 All the images annotations
my $annotFile = "/home/xy/face_sample/evaluation/compareROC/FDDB-folds/annotFile.txt";
4.5 The roc file you want to generate(created by evaluate exe):
my $gpFile ="/home/xy/face_sample/evaluation/compareROC/createROC.p";
4.6 You detection file (I will give how to create it latter)
my $detFile ="/home/xy/face_sample/evaluation/compareROC/detDir/fddb_rect_ret1.txt";
It’s content like that:
The ‘runEvaluate.pl’ have some error, change the execute evaluation to the below:
system($evaluateBin, "-a", $annotFile, "-d", $detFile, "-f", $detFormat, "-i", $imDir, "-l", $listFile, "-r", $detDir, "-z", ".jpg");
You can also use command to check it:
xy#xy:~/face_sample/evaluation/compareROC$ ./evaluate \
> -a /home/xy/face_sample/evaluation/compareROC/FDDB-folds/annotFile.txt \
> -d /home/xy/face_sample/evaluation/compareROC/detDir/fddb_rect_ret1.txt \
> -f 0 \
> -i /home/xy/face_sample/evaluation/compareROC/originalPics/ \
> -l /home/xy/face_sample/evaluation/compareROC/FDDB-folds/filePath.txt \
> -r /home/xy/face_sample/evaluation/compareROC/detDir/ \
> -z .jpg
Use python to create fddb evaluation txt file:
def get_img_relative_path():
"""
:return: ['2002/08/11/big/img_344', '2002/08/02/big/img_473', ......]
"""
f_name = 'E:/face_rec/face__det_rec_code/face_det/FDDB-folds/all_img_files.txt'
lst_name = open(f_name).read().split('\n')
return lst_name
def write_lines_to_txt(lst):
# lst = ['line1', 'line2', 'line3']
f_path = 'fddb_rect_ret.txt'
with open(f_path, 'w') as fp:
for line in lst:
fp.write("%s\n" % line)
# For example use opencv to face detection
def detect_face_lst(img):
"""
:param img: opencv image
:return: face rectangles [[x, y, w, h], ..........]
"""
m_path = 'D:/opencv/sources/data/haarcascades/haarcascade_frontalface_default.xml'
face_cascade = cv2.CascadeClassifier(m_path)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(gray, 1.3, 5)
return faces
def generate_fddb_ret():
# The directory from which we get the test images from FDDB
img_base_dir = 'E:/face_rec/face__det_rec_code/face_det/originalPics/'
# All the images relative path, like '['2002/08/11/big/img_344', '2002/08/02/big/img_473', ......]'
lst_img_name = get_img_relative_path()
# Store detect result, like:
# ['2002/08/11/big/img_344', '1', '10 10 50 50 1', .............]
lst_write2_fddb_ret = []
try:
for img_name in lst_img_name:
img_full_name = img_base_dir + img_name + '.jpg'
img = cv2.imread(img_full_name)
if img == None:
print 'error %s not exists, can not generate complete fddb evaluate file' % img_full_name
return -1
lst_face_rect = detect_face_lst(img)
# append img name like '2002/08/11/big/img_344'
lst_write2_fddb_ret.append(img_name)
face_num = len(lst_face_rect)
# append face num, note if no face 0 should be append
lst_write2_fddb_ret.append(str(face_num))
if face_num > 0:
# append each face rectangle x y w h score
for face_rect in lst_face_rect:
# append face rectangle x, y, w, h score
# note: opencv hava no confidence so use 1 here
s_rect = " ".join(str(item) for item in face_rect) + " 1"
lst_write2_fddb_ret.append(s_rect)
except Exception as e:
print 'error %s , can not generate complete fddb evaluate file' % e
return -1
# Write all the result to txt for FDDB evaluation
write_lines_to_txt(lst_write2_fddb_ret)
After run the above code you can create FDDB result:
Note: when you create the above txt in windows, if you test it in ubuntu you may get the following errorIncompatible annotation and detection files. See output specifications:
Just copy the content to a new txt file(created in ubuntu) then it solves.
Here is the result:
Some tips:
You can see the runEvaluate.pl it's not hard, the above changes may not be needed.You can also change some variable in runEvaluate.pl, like $GNUPLOT, $imDir and so on.
add "-z", ".jpg" to
system($evaluateBin, "-a", $annotFile, "-d", $detFile, "-f", $detFormat, "-i", $imDir, "-l", $listFile, "-r", $detDir);
system($evaluateBin, "-a", $annotFile, "-d", $detFile, "-f", $detFormat, "-i", $imDir, "-l", $listFile, "-r", $detDir, "-z", ".jpg");
You can also read the evaluate code(mainly the evaluate.cpp which is easy to understand ), so you will have a deep understand of how to evaluate it.
can you explain the step you are in?
You need to download the labelled data from:
http://vis-www.cs.umass.edu/fddb/ where it says: Download the database
After that you need to download the result source code:
http://vis-www.cs.umass.edu/fddb/results.html
Then you need to modify your program so that the output looks like this:
2002/08/11/big/img_591
1
191 88 164 163 0
2002/08/26/big/img_265
3
52 39 95 95 0
282 59 114 114 0
Where first is the name of the image,
then the number of faces in that image,
then the coordenates for each face and repeat...
I advice you to build the evaluation on linux since it's a lot easier (at least for me it was).
Hope it helps.
I have image in csv file and i want to load it in my program. I found that I can load image from cvs like this:
CvMLData mlData;
mlData.read_csv(argv[1]);
const CvMat* tmp = mlData.get_values();
cv::Mat img(tmp, true),img1;
img.convertTo(img, CV_8UC3);
cv::namedWindow("img");
cv::imshow("img", img);
I have RGB picture in that file but I got grey picture... Can somebody explain me how to load color image or how can I modify this code to get color image?
Thanks!
Updated
Ok, I don't know how to read your file into OpenCV for the moment, but I can offer you a work-around to get you started. The following will create a header for a PNM format file to match your CSV file and then append your data onto the end and you should end up with a file that you can load.
printf "P3\n284 177\n255\n" > a.pnm # Create PNM header
tr -d ',][' < izlaz.csv >> a.pnm # Append CSV data, after removing commas and []
If I do the above, I can see your bench, tree and river.
If you cannot read that PNM file directly into OpenCV, you can make it into a JPEG with ImageMagick like this:
convert a.pnm a.jpg
I also had a look at the University of Wisconsin ML data archive, that is read with those OpenCV functions that you are using, and the format of their data is different from yours... theirs is like this:
1000025,5,1,1,1,2,1,3,1,1,2
1002945,5,4,4,5,7,10,3,2,1,2
1015425,3,1,1,1,2,2,3,1,1,2
1016277,6,8,8,1,3,4,3,7,1,2
yours looks like this:
[201, 191, 157, 201 ... ]
So maybe this tr command is enough to convert your data:
tr -d '][' < izlaz.csv > TryMe.csv
Original Answer
If you run the following on your CSV file, it translates commas into newlines and then counts the lines:
tr "," "\n" < izlaz.csv | wc -l
And that gives 150,804 lines, which means 150,804 commas in your file and therefore 150,804 integers in your file (+/- 1 or 2). If your greyscale image is 177 rows by 852 columns, you are going to need 150,804 RGB triplets (i.e. 450,000 +/- integers) to represent a colour image, as it is you only have a single greyscale value for each pixel.
The fault is in the way you write the file, not the way you read it.
To see color image I must set number of channels. So this code works for me:
CvMLData mlData;
mlData.read_csv(argv[1]);
const CvMat* tmp = mlData.get_values();
cv::Mat img(tmp, true),img1;
img.convertTo(img, CV_8UC3);
img= img.reshape(3); //set number of channels
I want to detect the flag of my country. But I have trouble with training. I have one positive sample and 4 negative samples. This is my folder structrue:
/negative
/img
img1.jpg
img2.jpg
img3.jpg
img4.jpg
/positive
flag.jpg
This is how I call create_samples:
opencv_createsamples -img positive/flag.jpg -vec flag.vec
But command does not finish and popup windows appears saying that error appeared. This is output of create_samples command:
Info file name: (NULL)
Img file name: positive/flag.jpg
Vec file name: flag.vec
BG file name: (NULL)
Num: 1000
BG color: 0
BG threshold: 80
Invert: FALSE
Max intensity deviation: 40
Max x angle: 1.1
Max y angle: 1.1
Max z angle: 0.5
Show samples: FALSE
Width: 24
Height: 24
Create training samples from single image applying distortions...
Can anyone guide me through the process of haar training of static image (1 image) in OpenCV? I am running Windows 7 Ultimate x64
EDIT
This works:
opencv_createsamples -img positive/flag.jpg -vec flag.vec -num 0
I guess the problem is with -num 0 parameter
Haar is not your best shot at detecting a country's flag , better use color detection
COLOR_MIN = np.array([20, 80, 80],np.uint8)
COLOR_MAX = np.array([40, 255, 255],np.uint8)
but if you still insist on haar
creating samples :
$ <opencv_createsamples> -vec <binary_description> -image <positive_image> -bg <negative_description>
Training the cascade :
$ <opencv_traincascade> -data <cascade> -vec <binary_description> -bg <negative_description>