
<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
		<id>https://wiki.robotika.sk/robowiki/index.php?action=history&amp;feed=atom&amp;title=Door_Camera_-_Code</id>
		<title>Door Camera - Code - Revision history</title>
		<link rel="self" type="application/atom+xml" href="https://wiki.robotika.sk/robowiki/index.php?action=history&amp;feed=atom&amp;title=Door_Camera_-_Code"/>
		<link rel="alternate" type="text/html" href="https://wiki.robotika.sk/robowiki/index.php?title=Door_Camera_-_Code&amp;action=history"/>
		<updated>2026-05-05T03:34:39Z</updated>
		<subtitle>Revision history for this page on the wiki</subtitle>
		<generator>MediaWiki 1.30.0</generator>

	<entry>
		<id>https://wiki.robotika.sk/robowiki/index.php?title=Door_Camera_-_Code&amp;diff=11345&amp;oldid=prev</id>
		<title>Robot: Created page with &quot;Return back to project page:  Door camera - Jakub Vojtek  Python code for the Door Camera project: &lt;syntaxhighlight lang=python&gt;...&quot;</title>
		<link rel="alternate" type="text/html" href="https://wiki.robotika.sk/robowiki/index.php?title=Door_Camera_-_Code&amp;diff=11345&amp;oldid=prev"/>
				<updated>2024-06-09T16:47:18Z</updated>
		
		<summary type="html">&lt;p&gt;Created page with &amp;quot;Return back to project page: &lt;a href=&quot;/robowiki/index.php?title=Spike_Prime_-_Door_camera_-_Jakub_Vojtek&quot; title=&quot;Spike Prime - Door camera - Jakub Vojtek&quot;&gt; Door camera - Jakub Vojtek&lt;/a&gt;  Python code for the Door Camera project: &amp;lt;syntaxhighlight lang=python&amp;gt;...&amp;quot;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;Return back to project page: [[Spike Prime - Door camera - Jakub Vojtek| Door camera - Jakub Vojtek]]&lt;br /&gt;
&lt;br /&gt;
Python code for the Door Camera project:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=python&amp;gt; &lt;br /&gt;
import cv2&lt;br /&gt;
from buildhat import DistanceSensor&lt;br /&gt;
from datetime import datetime&lt;br /&gt;
import threading&lt;br /&gt;
import sqlite3&lt;br /&gt;
import os&lt;br /&gt;
import time&lt;br /&gt;
&lt;br /&gt;
distance_sensor = DistanceSensor('A')&lt;br /&gt;
&lt;br /&gt;
video_capture = cv2.VideoCapture(0)&lt;br /&gt;
frame_width = 480&lt;br /&gt;
frame_height = 360&lt;br /&gt;
video_capture.set(cv2.CAP_PROP_FRAME_WIDTH, frame_width)&lt;br /&gt;
video_capture.set(cv2.CAP_PROP_FRAME_HEIGHT, frame_height)&lt;br /&gt;
&lt;br /&gt;
# face detector&lt;br /&gt;
face_classifier = cv2.CascadeClassifier(cv2.data.haarcascades + &amp;quot;haarcascade_frontalface_default.xml&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
current_frame = None&lt;br /&gt;
lock = threading.Lock()&lt;br /&gt;
&lt;br /&gt;
# create camera_pictures directory if it doesn't exist&lt;br /&gt;
if not os.path.exists(&amp;quot;camera_pictures&amp;quot;):&lt;br /&gt;
    os.makedirs(&amp;quot;camera_pictures&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
db_directory = &amp;quot;database&amp;quot;&lt;br /&gt;
if not os.path.exists(db_directory):&lt;br /&gt;
    os.makedirs(db_directory)&lt;br /&gt;
&lt;br /&gt;
db_filename = os.path.join(db_directory, &amp;quot;face_images.db&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
conn = sqlite3.connect(db_filename)&lt;br /&gt;
c = conn.cursor()&lt;br /&gt;
&lt;br /&gt;
c.execute('''&lt;br /&gt;
    CREATE TABLE IF NOT EXISTS images (&lt;br /&gt;
        id INTEGER PRIMARY KEY AUTOINCREMENT,&lt;br /&gt;
        timestamp TEXT NOT NULL,&lt;br /&gt;
        detected_faces INTEGER NOT NULL&lt;br /&gt;
    )&lt;br /&gt;
''')&lt;br /&gt;
conn.commit()&lt;br /&gt;
&lt;br /&gt;
def camera_thread():&lt;br /&gt;
    global current_frame&lt;br /&gt;
    while True:&lt;br /&gt;
        result, frame = video_capture.read()&lt;br /&gt;
        if result:&lt;br /&gt;
            with lock:&lt;br /&gt;
                current_frame = frame&lt;br /&gt;
&lt;br /&gt;
def take_and_save_picture():&lt;br /&gt;
    with lock:&lt;br /&gt;
        if current_frame is not None:&lt;br /&gt;
            video_frame = current_frame.copy()&lt;br /&gt;
            gframe = cv2.cvtColor(video_frame, cv2.COLOR_BGR2GRAY)&lt;br /&gt;
&lt;br /&gt;
            faces = face_classifier.detectMultiScale(gframe, 1.1, 5, minSize=(40, 40))&lt;br /&gt;
&lt;br /&gt;
            detected_faces = len(faces)&lt;br /&gt;
            if detected_faces &amp;gt; 0:&lt;br /&gt;
                faces = sorted(faces, key=lambda x: x[2] * x[3], reverse=True)&lt;br /&gt;
                x, y, width, height = faces[0]&lt;br /&gt;
                cv2.rectangle(video_frame, (x, y), (x + width, y + height), (0, 255, 0), 4)&lt;br /&gt;
            else:&lt;br /&gt;
                cv2.putText(video_frame, &amp;quot;Face couldn't be detected&amp;quot;, (video_frame.shape[1] - 250, video_frame.shape[0] - 10),&lt;br /&gt;
                            cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 1, cv2.LINE_AA)&lt;br /&gt;
&lt;br /&gt;
            timestamp = datetime.now().strftime('%Y-%m-%d %H:%M:%S')&lt;br /&gt;
            cv2.putText(video_frame, timestamp, (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2, cv2.LINE_AA)&lt;br /&gt;
&lt;br /&gt;
            filename = f&amp;quot;camera_pictures/face_detected_{datetime.now().strftime('%Y%m%d_%H%M%S')}.jpg&amp;quot;&lt;br /&gt;
            cv2.imwrite(filename, video_frame)&lt;br /&gt;
            print(f&amp;quot;Saved image: {filename}&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
            # save time and amount of detected faces to database&lt;br /&gt;
            c.execute('''&lt;br /&gt;
                INSERT INTO images (timestamp, detected_faces)&lt;br /&gt;
                VALUES (?, ?)&lt;br /&gt;
            ''', (timestamp, detected_faces))&lt;br /&gt;
            conn.commit()&lt;br /&gt;
&lt;br /&gt;
            time.sleep(1)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
if __name__ == '__main__':&lt;br /&gt;
    threading.Thread(target=camera_thread, daemon=True).start()&lt;br /&gt;
&lt;br /&gt;
    try:&lt;br /&gt;
        while True:&lt;br /&gt;
            distance = distance_sensor.get_distance()&lt;br /&gt;
            if distance &amp;lt; 300:&lt;br /&gt;
                take_and_save_picture()&lt;br /&gt;
    finally:&lt;br /&gt;
        video_capture.release()&lt;br /&gt;
        cv2.destroyAllWindows()&lt;br /&gt;
        conn.close()&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;/div&gt;</summary>
		<author><name>Robot</name></author>	</entry>

	</feed>