This project demonstrates how to train a custom Haar Cascade classifier using OpenCV to detect and censor offensive symbols in images or video streams. The system supports both static image detection and real-time webcam detection.
git clone https://github.com/mrnugget/opencv-haar-classifier-training
cd opencv-haar-classifier-training
Collect 20โ40 positive images containing the symbol and 600โ800 negative images without the symbol. Place them into:
find ./positive_images -iname "*.jpg" > positives.txt
find ./negative_images -iname "*.jpg" > negatives.txt
perl bin/createsamples.pl positives.txt negatives.txt samples 1500
cd tools
python mergevec.py -v /path/to/samples/ -o samples.vec
opencv_traincascade -data classifier -vec samples.vec -bg negatives.txt -numStages 20 -minHitRate 0.999 -maxFalseAlarmRate 0.5 -numPos 1000 -numNeg 600 -w 80 -h 40 -mode ALL
After training completes, your custom classifier will be located at:
classifier/cascade.xml
cascade = cv2.CascadeClassifier('cascade.xml')
image = cv2.imread('test.jpg')
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
detections = cascade.detectMultiScale(gray, 1.3, 5)
for (x, y, w, h) in detections:
cv2.rectangle(image, (x,y), (x+w,y+h), (0,255,0), 2)
python detect_using_webcam.py
roi = image[y:y+h, x:x+w]
blur = cv2.GaussianBlur(roi, (99, 99), 30)
image[y:y+h, x:x+w] = blur
Successfully built and trained a custom Haar Cascade classifier capable of detecting offensive symbols in both images and live video. The system can automatically censor detected regions, demonstrating practical computer vision and classical machine learning techniques.
GitHub Repository: https://github.com/aterakrofi/Haarcascade-to-detect-swastika
โ Back to Resume