Cascade classifier for identifying screws (1)


foreword

Because of a project of my tutor, I recently learned how to train a target recognizer based on cascade classification. The author takes screws as an example to train a screw recognizer based on cascade classification.

1. Introduction to Cascaded Classifiers

Cascaded classifiers are a tree-based technique. It is built primarily on the important concept of boosted rejection cascading. This technology has been very successfully applied to face detection, and of course it can also be applied to the detection of other targets. Generally speaking, targets with rigid structures and strong texture features will get good detection results using this method. The screws selected in this article belong to rigid bodies, but because the individual screws are small, the author's first detection effect is not very good. Without further ado, let's go to the code.

2. Training steps

1. Collect images

The author used the images collected by the Logitech camera, and the light source and background were not specially set when shooting. The collected images are shown in Fig.
insert image description here

2. Image processing

2.1 Image file renaming

The naming of image files collected by the camera is very messy, which is not conducive to subsequent batch processing. I wrote a renaming script using python

def rename(path1):
    '''
    对文件夹中文件进行批量重命名,文件名为1.jpg,2.jpg。。。。。。这种形式。
    参数解释,
    path1:以绝对路径传入文件路径
    '''
    # path1 = 'C:\\Users\\Administrator\\Desktop\\logitech'
    filename = os.listdir(path1)
    print(len(filename))
    for i,name in enumerate(filename):
        i+=1
        st = str(i)+'.jpg'
        os.renames(os.path.join(path1,name),os.path.join(path1,st))

2.2 Cropping the ROI area

This step can be said to be the only process in the entire recognition process that cannot be automated with code, because in this step, you need to cut out the screw part from the original image. If this step can also be automated with code, then it is unnecessary. Then train a trainer that recognizes screws. The image after cropping is as follows

insert image description here

2.3 Batch grayscale and compression

This step needs to compress the above pictures to a smaller size (if the size is too large, the computer cannot run, and friends with extremely high computer configuration can automatically ignore it), generally speaking, it is enough to compress to 40X40, but because of the problem of the author taking pictures, the original The aspect ratio of the picture is not consistent, and the compression to 40X40 is too distorted, so the author here is compressed to 80X80, and all grayscale processing is required after compression. Here the author also wrote a script in python (what can be done by a computer, don't need to be done by humans).

def cut(path):
    filename = os.listdir(path)
    
    for i,name in enumerate(filename):
        i+= 1
        i = str(i)
        imgpath = path + '\\' + name
        print(imgpath)
        o = cv2.imread(imgpath)
        o = cv2.cvtColor(o,cv2.COLOR_BGR2GRAY)
        img = cv2.resize(o,(80,80))
        cv2.imwrite('C:\\Users\\Administrator\\Desktop\\bolt\\' + i +'.jpg' ,img)
        

3 Make sample data

3.1 Making positive samples

We have obtained the processed screw picture, and now we need to create a positive sample set description file, which is used to prepare for the positive sample vector file generated by the createsample program later. The set description file as shown in the
insert image description here
figure pos/1.jpg represents the relative address of the picture, 1 represents the number of positive examples (referred to as screws in this article) in the picture, and 0 0 represents the coordinates of the upper left corner of the target in the picture. After correct cropping, it is generally (0,0), 80,80 represent the length and width of the target respectively.

3.2 Negative Sample Production

There is only one requirement for negative sample making, which cannot contain the target object. Others are more random, the size of the picture does not need to be the same (too large is not good, the computer cannot run), and there is no need to cut it, just make another collection file. In the negative sample description file, there is only the
insert image description here
relative address of the picture. There are no other parameters.

4. Generate positive sample vector files

Put all the files obtained above in the same folder, as shown
insert image description here
in the figure, you also need to add the two exe (opencv_createsamples, opencv_traincascade) that comes with opencv for training cascade classification and the dlls they depend on (opencv_world342, opencv_world342d ) in that folder.
The next step is to use opencv_createsamples to generate the positive sample vector file.
First, open the cmd platform, and use the cd command to go to the folder where the target file is located (this step is very important, if you do not go to the target folder in advance, it may be found when the vector file is generated later. The error of not seeing the picture)
insert image description here
and then run a bat file to generate the positive sample vector file. The
content of the bat file is as follows:
opencv_createsamples.exe -vec posvec.vec -info pos.txt -num 50 -w 80 -h 80
pause
Among them, opencv_createsamples.exe means you want to run this program,
vec posvec.vec means you want to generate a vector file named posvec, save it in your cd past folder, info pos.txt means the collection description file of positive samples, num 50 means There are 50 positive samples, and w 80 and h 80 represent the length and width of the sample, respectively. There are many other parameters in this program, interested friends can refer to the book of learning opencv3. After running,
insert image description here
there will be a posvec.vec vector file in the folder at the same time
Negative samples do not need to generate vector files, just use them directly

5. Start training

The required data is ready, and then you only need to use the opencv_traincascade program to train your own classifier.
Still the same as above, write a bat batch file for automatic operation, the contents of the five files are as follows

opencv_traincascade.exe -data bolt -vec posvec.vec -bg neg.txt -numPos 50 -numNeg 666 -numStages 10 -w 80 -h 80 -minHitRate 0.999 -maxFalseAlarmRate 0.2 -weightTrimRate 0.95 -featureType LBP
pause

Among them, data bolt represents that the training parameter file will be saved in the bolt folder (this means that you must first create a bolt folder in the target folder, otherwise an error will be reported), bg neg.txt represents the collection file of negative samples, numPos 50 means the number of positive samples is 50, numNeg 666 means the number of negative samples is 666, numStages 10 means the number of training rounds is 10 rounds, minHitRate 0.999 means the minimum hit rate is 0.999, (ideally it should be 100%, but the algorithm cannot achieve Up to this point, so it is generally above 0.995), maxFalseAlarmRate 0.2 represents the maximum false alarm rate of 0.2 (ideally, the value should be 0, in practice this is still not possible, generally below 0.5), weightTrimRate 0.95 represents The parameters of the boosting algorithm, the default is 0.95, featureType LBP represents the selected feature type, currently the cascade classifier supports haar and lbp two feature types, lbp is used more.

Drag the bat batch file to the cmd command console, and the program will start training. The training time varies depending on the configuration of the personal computer. The computer configuration of the author is poor (i5 processor, 4G storage), and it took nearly 100 minutes. get a file like this

insert image description here
Among them, bolt_cascade (the default is cascade, the author renamed it) is the xml file we need.

6. Results

Use the trained xml file for recognition, the recognition code is as follows

from cv2 import cv2
	 
# 加载opencv自带的人脸分类器
# faceCascade = cv2.CascadeClassifier(r"C:\Users\Administrator\Desktop\haarcascade_frontalface_alt2.xml")
# faceCascade.load(r'C:\Users\Administrator\Desktop\haarcascade_frontalface_alt2.xml')

faceCascade = cv2.CascadeClassifier(r"C:\Users\Administrator\Desktop\cascade_bolt.xml")
faceCascade.load(r'C:\Users\Administrator\Desktop\cascade_bolt.xml') 
cap = cv2.VideoCapture(0)
flag = 0
timeF = 4
i=0
while True:
	flag+=1
	ret, frame = cap.read()
	img = frame.copy()
	gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
	rect = faceCascade.detectMultiScale(
	        gray,
	        scaleFactor=1.15,
	        minNeighbors=3,
	        minSize=(3,3),
	        flags = cv2.IMREAD_GRAYSCALE
	    )
	for (x, y, w, h) in rect:
	    i+=1
	    cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 255, 0), 2)
	    # 识别到物体后进行裁剪保存
	    # tongue = img[x:(x+w), y:(y+h)]
	    if (flag%timeF == 0):
	        cv2.imwrite('C:\\Users\\Administrator\\Desktop\\boltimg\\bolt%d.jpg' % i,frame) 
 
	cv2.imshow('frame', frame)
	if cv2.waitKey(1) & 0xFF == ord('q'):
	    break
cap.release()
cv2.destroyAllWindows()

The recognition effect is not very good, 44 pictures were recognized, only 6 were accurately recognized, and the accuracy rate was only 14%.
insert image description here

Summarize

Although the recognition effect is poor, the author has mastered how to train a cascade classifier through this training, and will optimize this screw classifier later.
Points to be improved:
First, the aspect ratio of the photos is different, resulting in some image distortions during batch compression.
Second, the background selection is not good when taking pictures. You should try to use a white screen to reduce background interference.
Third, the number of positive samples is not enough, only 50, and the number of negative samples can continue to increase.
Improvements will be made on these points in the future.

Guess you like

Origin blog.csdn.net/bookshu6/article/details/111245484