From 0529be0cd1339dc95c1c53f188a2c36dea435ba3 Mon Sep 17 00:00:00 2001 From: NotArt Date: Fri, 7 Mar 2025 05:32:53 +0100 Subject: [PATCH] + | Camera tests --- Camera_test/Camera_test.ino | 115 --------------------------------- Camera_test/chat.ino | 22 ------- Camera_test/faceDetectionAI.py | 60 +++++++++++------ 3 files changed, 42 insertions(+), 155 deletions(-) delete mode 100644 Camera_test/Camera_test.ino delete mode 100644 Camera_test/chat.ino diff --git a/Camera_test/Camera_test.ino b/Camera_test/Camera_test.ino deleted file mode 100644 index 87c1a48..0000000 --- a/Camera_test/Camera_test.ino +++ /dev/null @@ -1,115 +0,0 @@ -// #include - -// // OV7670 pin definitions (modify if needed) -// #define VSYNC_PIN 2 -// #define HREF_PIN 3 -// #define PCLK_PIN 4 -// #define XCLK_PIN 5 - -// // Data pins (D0-D7 connected to Arduino) -// int dataPins[] = {6, 7, 8, 9, 10, 11, 12, 13}; - -// // OV7670 Camera Initialization -// void setup() { -// Serial.begin(115200); -// delay(2000); -// Serial.println("Serial begin"); - -// Wire.begin(); // Initialize I2C -// delay(500); -// Serial.println("Wire begin"); - -// Serial.println("I2C Scanner") ; -// for (byte address = 1; address < 127; address++) { -// Wire.beginTransmission(address); -// if (Wire.endTransmission() == 0) { -// Serial.print("Device found at 0x"); -// Serial.println(address, HEX); -// } -// } - -// Serial.println("End of I2C Scanner"); - -// pinMode(LED_BUILTIN, OUTPUT); -// for (int i = 0; i < 5; i++) { // Blink LED 5 times -// digitalWrite(LED_BUILTIN, HIGH); -// delay(500); -// digitalWrite(LED_BUILTIN, LOW); -// delay(500); -// } - -// Serial.println("Setup started..."); // Debug message - -// pinMode(VSYNC_PIN, INPUT); -// pinMode(HREF_PIN, INPUT); -// pinMode(PCLK_PIN, INPUT); -// pinMode(XCLK_PIN, OUTPUT); - -// // Set data pins as input -// for (int i = 0; i < 8; i++) { -// pinMode(dataPins[i], INPUT); -// } - -// // Initialize OV7670 -// Serial.println("Initializing Camera..."); -// if (!initCamera()) { -// Serial.println("Camera init failed!"); -// while (1); // Stop if camera does not initialize -// } -// Serial.println("Camera Ready!"); -// } - -// // Function to Initialize Camera (Modify If Needed) -// bool initCamera() { -// Wire.beginTransmission(0x21); // OV7670 I2C address (check datasheet) -// Wire.write(0x12); // Register to reset the camera -// Wire.write(0x80); // Reset command -// if (Wire.endTransmission() != 0) return false; - -// delay(100); - -// // Set camera to QVGA (320x240) or other settings if needed -// Wire.beginTransmission(0x21); -// Wire.write(0x12); -// Wire.write(0x14); // Set to QVGA -// return (Wire.endTransmission() == 0); -// } - -// // Capture a Frame -// void captureFrame() { -// Serial.println("Capturing Frame..."); - -// while (digitalRead(VSYNC_PIN) == LOW); // Wait for VSYNC high -// while (digitalRead(VSYNC_PIN) == HIGH); // Wait for VSYNC low - -// // Read each pixel -// for (int y = 0; y < 120; y++) { // Capture 120 rows (for QVGA) -// while (digitalRead(HREF_PIN) == LOW); // Wait for HREF high - -// for (int x = 0; x < 160; x++) { // Read 160 pixels per row -// while (digitalRead(PCLK_PIN) == LOW); // Wait for PCLK high -// int pixel = readPixel(); // Read pixel data -// Serial.print(pixel); -// Serial.print(" "); -// while (digitalRead(PCLK_PIN) == HIGH); // Wait for PCLK low -// } - -// Serial.println(); // New row -// } - -// Serial.println("Frame Captured!"); -// } - -// // Read a Pixel from the Camera -// int readPixel() { -// int pixel = 0; -// for (int i = 0; i < 8; i++) { -// pixel |= digitalRead(dataPins[i]) << i; // Read data pins -// } -// return pixel; -// } - -// void loop() { -// captureFrame(); // Capture and print pixel data -// delay(5000); // Wait before capturing next frame -// } diff --git a/Camera_test/chat.ino b/Camera_test/chat.ino deleted file mode 100644 index cab8a3a..0000000 --- a/Camera_test/chat.ino +++ /dev/null @@ -1,22 +0,0 @@ -#include - -void setup() { - Serial.begin(115200); - delay(2000); // Wait for the serial monitor to open - Serial.println("I2C Scanner"); - - Wire.begin(); // Initialize I2C communication - for (byte address = 1; address < 127; address++) { - Wire.beginTransmission(address); - if (Wire.endTransmission() == 0) { - // If the device responds, it means it’s present on the bus - Serial.print("Device found at address 0x"); - Serial.println(address, HEX); - } - } - Serial.println("End of I2C Scanner"); -} - -void loop() { - // Empty loop -} diff --git a/Camera_test/faceDetectionAI.py b/Camera_test/faceDetectionAI.py index 3a3c1f2..a97b953 100644 --- a/Camera_test/faceDetectionAI.py +++ b/Camera_test/faceDetectionAI.py @@ -1,4 +1,7 @@ import cv2 +import serial + +SERVO_SPD = 2 def draw_square(frame, faces, color): for (x, y, w, h) in faces: @@ -17,7 +20,8 @@ detected_profiles = [] ret, frame = video_capture.read() if ret: frameH, frameW = frame.shape[:2] - + eyeX = int (frameW / 2) + eyeY = int (frameH / 2) while True: ret, frame = video_capture.read() @@ -30,26 +34,46 @@ while True: faces = side_cascade.detectMultiScale(gray_frame, scaleFactor=1.1, minNeighbors=5, minSize=(30, 30)) detected_profiles = faces - target = [0, 0, 0, 0] - if len(detected_faces) != 0 : - for face in detected_faces : - if face[3] > target[3] : - target = face - - print(f"{target[3]}") - - # print(f"midH={frameH} midW={frameW} {len(detected_faces)} {len(detected_profiles)} ") - frame = draw_square(frame, detected_faces, (0, 255, 0)) frame = draw_square(frame, detected_profiles, (255, 0, 0)) - if len(detected_faces) != 0 : - targetX = int (target[0] + (target[2] / 2)) - targetY = int (target[1] + (target[3] / 2)) - else : - targetX = int (frameW / 2) - targetY = int (frameH / 2) - cv2.rectangle(frame, (targetX, targetY), (targetX + 2, targetY + 2), (0, 0, 255), 1) + target = [int ((frameW / 2) - 10), int ((frameH / 2) - 10), 20, 20] + if len(detected_profiles) != 0 and len(detected_faces) == 0 : + # get target as the biggest face detected + for face in detected_profiles : + if face[3] > target[3] : # deep copy as I want to modify target w/out modifying detected_profiles + target[0] = face[0] + target[1] = face[1] + target[2] = face[2] + target[3] = face[3] + + # define target as the center of selected face + target[0] = int (target[0] + (target[2] / 2) - 10) + target[1] = int (target[1] + (target[3] / 2) - 10) + target[2] = 20 + target[3] = 20 + + # adjust the 'eye' position as the camera turn around + if eyeX < target[0] or eyeX > (target[0] + target[2]) : + if eyeX < target[0] : + eyeX += SERVO_SPD + else : + eyeX -= SERVO_SPD + if eyeY < target[1] or eyeY > (target[1] + target[3]) : + if eyeY < target[1] : + eyeY += SERVO_SPD + else : + eyeY -= SERVO_SPD + + + # draw target rectangle in red + cv2.rectangle(frame, (target[0], target[1]), (target[0] + target[2], target[1] + target[3]), (0, 0, 255), 1) + + # draw crosshair in white + cv2.rectangle(frame, (eyeX - 5, eyeY), (eyeX + 5, eyeY), (255, 255, 255), 1) + cv2.rectangle(frame, (eyeX, eyeY - 5), (eyeX, eyeY + 5), (255, 255, 255), 1) + + cv2.imshow("Faces", frame)