โปรแกรมตรวจจับใบหน้าด้วย OpenCV และ Python ที่รองรับการตรวจจับใบหน้าจากหลายแหล่งที่มา ทั้งไฟล์ภาพ, URL, และกล้องแบบ Real-time
📝 Latest Version: v1.0.0 - Face Detection with Multiple Input Sources
- 📁 Local Image - ตรวจจับใบหน้าจากไฟล์ภาพในเครื่อง
- 🌐 URL Image - ดาวน์โหลดและตรวจจับใบหน้าจากภาพออนไลน์
- 📹 Real-time Camera - ตรวจจับใบหน้าแบบ real-time จากกล้อง webcam
- 🤖 Haar Cascade Classifier - ใช้โมเดล pre-trained สำหรับตรวจจับใบหน้า
- ⚡ Fast Processing - ประมวลผลรวดเร็วด้วย OpenCV
- 🎨 Visual Feedback - แสดงกรอบสีเหลืองรอบใบหน้าที่ตรวจพบ
- 🔄 Multi-face Detection - ตรวจจับหลายใบหน้าในภาพเดียว
- 📱 Multi-camera Support - รองรับการเลือกกล้องหลายตัว
- OpenCV (cv2) - Computer Vision library สำหรับการประมวลผลภาพ
- NumPy - การจัดการ array และข้อมูลภาพ
- urllib - ดาวน์โหลดภาพจาก URL
facedetection/
├── detect.py # Main application file
├── facedetectionmodel.xml # Haar Cascade model file
├── requirements.txt # Python dependencies
└── README.md # Documentation
- Python 3.7+
- pip สำหรับติดตั้ง packages
- Webcam (ถ้าต้องการใช้งานโหมดกล้อง)
- facedetectionmodel.xml - Haar Cascade model file
git clone https://github.com/xenodeve/facedetection.git
cd facedetectionpip install -r requirements.txtหรือติดตั้งแบบ manual:
pip install opencv-python==4.10.0.84
pip install numpy==1.26.4# ดาวน์โหลดไฟล์ haarcascade_frontalface_default.xml จาก OpenCV
# และเปลี่ยนชื่อเป็น facedetectionmodel.xmlหรือดาวน์โหลดจาก: OpenCV GitHub - Haar Cascades
python detect.pyWhere do you get the input?
0 = local
1 = url
2 = cam
Answer(Number) = 0
- ตรวจจับใบหน้าจากไฟล์
tag.ongในโฟลเดอร์เดียวกัน - แสดงผลภาพพร้อมกรอบใบหน้า
- กด ปุ่มใดก็ได้ เพื่อปิดหน้าต่าง
Where do you get the input?
0 = local
1 = url
2 = cam
Answer(Number) = 1
url = https://example.com/image.jpg
- ใส่ URL ของภาพที่ต้องการตรวจจับใบหน้า
- โปรแกรมจะดาวน์โหลดและตรวจจับใบหน้าอัตโนมัติ
- กด ปุ่มใดก็ได้ เพื่อปิดหน้าต่าง
Where do you get the input?
0 = local
1 = url
2 = cam
Answer(Number) = 2
Which camera?
(if you have only 1 cam. enter 0, Default = 0)
Answer = 0
- เลือกหมายเลขกล้องที่ต้องการใช้ (ปกติใช้ 0)
- ตรวจจับใบหน้าแบบ real-time
- กด 'q' เพื่อออกจากโปรแกรม
- detection() - สำหรับภาพนิ่ง (local และ URL)
- detection2() - สำหรับ video stream (camera)
- สีกรอบ: เหลือง (BGR: 255, 255, 0)
- ความหนากรอบ: 2 pixels
- การแปลงสี: Gray scale สำหรับการตรวจจับ
# สำหรับภาพนิ่ง
faces = face_model.detectMultiScale(gray_scale)
# สำหรับ video stream
faces = face_model.detectMultiScale(gray_scale, 1.3, 5)
# scaleFactor = 1.3
# minNeighbors = 5# ดาวน์โหลด Haar Cascade model
wget https://raw.githubusercontent.com/opencv/opencv/master/data/haarcascades/haarcascade_frontalface_default.xml -O facedetectionmodel.xml# ลองเปลี่ยนหมายเลขกล้อง
# 0 = กล้องหลัก
# 1 = กล้องรอง (ถ้ามี)- ตรวจสอบแสงสว่างในภาพ
- ลองปรับ parameters ใน
detectMultiScale() - ใบหน้าควรหันตรงกล้อง
# ติดตั้ง opencv-python ใหม่
pip uninstall opencv-python
pip install opencv-python==4.10.0.84# เปลี่ยนสีกรอบใน detect.py
cv.rectangle(img, (x,y), (x+w,y+h), (0,255,0), 2) # สีเขียว
cv.rectangle(img, (x,y), (x+w,y+h), (255,0,0), 2) # สีน้ำเงิน# ปรับ parameters สำหรับความแม่นยำที่ดีขึ้น
faces = face_model.detectMultiScale(
gray_scale,
scaleFactor=1.1, # ค่าน้อย = แม่นยำมากขึ้น แต่ช้าลง
minNeighbors=5, # ค่ามาก = ลด false positive
minSize=(30, 30) # ขนาดใบหน้าขั้นต่ำ
)# บันทึกภาพที่ตรวจจับแล้ว
cv.imwrite('detected_faces.jpg', img)- Type: Pre-trained face detection model
- Algorithm: Viola-Jones object detection framework
- Training: Trained on thousands of positive and negative images
- Performance: Fast and efficient for frontal face detection
- รองรับการตรวจจับจากหลายแหล่งที่มา
- ง่ายต่อการใช้งานด้วย CLI interface
- เหมาะสำหรับทั้งการเรียนรู้และการใช้งานจริง
- ใช้ OpenCV ที่มีประสิทธิภาพสูง
- ตรวจจับ real-time ได้อย่างลื่นไหล
- รองรับหลายใบหน้าในภาพเดียว
- รองรับการตรวจจับใบหน้าด้านข้าง
- เพิ่มการจดจำใบหน้า (Face Recognition)
- บันทึก video output
- GUI interface
- การตรวจจับอารมณ์ (Emotion Detection)
- การตรวจจับอายุและเพศ
- Export ผลลัพธ์เป็น JSON
XenoDeve
- GitHub: @xenodeve
- Repository: facedetection
ยินดีรับ Pull Requests และ Issues ทุกรูปแบบ!
- Fork โปรเจ็กต์
- สร้าง Feature Branch (
git checkout -b feature/AmazingFeature) - Commit การเปลี่ยนแปลง (
git commit -m 'Add some AmazingFeature') - Push ไปยัง Branch (
git push origin feature/AmazingFeature) - เปิด Pull Request
This project is licensed under the MIT License - see the LICENSE file for details.
- OpenCV Team - สำหรับ computer vision library ที่ยอดเยี่ยม
- Viola-Jones - สำหรับ object detection algorithm
- Python Community - สำหรับเครื่องมือและ libraries ที่ดีเยี่ยม
หากมีคำถามหรือปัญหาในการใช้งาน:
- 📧 เปิด Issue บน GitHub
- 💬 ติดต่อผ่าน GitHub Profile
🚀 Happy Coding! 👤