Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improvements #5

Merged
merged 13 commits into from
Apr 11, 2024
5 changes: 2 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,5 @@ cython_debug/
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
# and can be added to the global gitignore or merged into this file. For a more nuclear
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
#.idea/
DataCollector/apikey
config.yaml
.idea/
/**/config.yaml
36 changes: 0 additions & 36 deletions DataCollector/capture_and_send.py

This file was deleted.

87 changes: 0 additions & 87 deletions main.py

This file was deleted.

8 changes: 5 additions & 3 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
opencv-python
google-api-python-client
telebot
opencv-python~=4.9.0.80
google-api-python-client~=2.125.0
pyTelegramBotAPI~=4.16.1
PyYAML~=6.0.1
Pillow~=10.3.0
File renamed without changes.
39 changes: 39 additions & 0 deletions src/data_collector/camera_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import cv2
from PIL import Image

camera: cv2.VideoCapture | None = None


def get_camera(camera_port: int = 0) -> bool:
"""
This function initializes the camera

:param camera_port: The camera port
:return: True if the camera was initialized successfully, False otherwise
"""
global camera
camera = cv2.VideoCapture(camera_port)
if camera is None or not camera.isOpened():
camera = None
return False
camera.set(3, 1280)
camera.set(4, 960)
return True


def release_camera() -> None:
"""
This function releases the camera
"""
global camera
camera.release()
camera = None


def get_camera_image() -> Image.Image | None:
"""
This function captures an image from the camera
:return: A PIL Image object if the image was captured successfully, None otherwise
"""
result, image = camera.read()
return Image.fromarray(cv2.cvtColor(image, cv2.COLOR_BGR2RGB)) if result else None
14 changes: 7 additions & 7 deletions DataCollector/gdrive.py → src/data_collector/gdrive.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import os
import io
from google.oauth2 import service_account
from googleapiclient.discovery import build
from googleapiclient.http import MediaIoBaseUpload
from PIL.Image import Image
import datetime

# Define the Google Drive API scopes and service account file path
SCOPES = ["https://www.googleapis.com/auth/drive"]
SERVICE_ACCOUNT_FILE = "./DataCollector/apikey/datacollector-419114-bdc0fad0e57d.json"
SERVICE_ACCOUNT_FILE = "./datacollector-419114-bdc0fad0e57d.json"
PARENT_FOLDER_ID = "1ne0IuBRl5cK_PeAnpU8PIaaRpVKQ1lFB"
# Create credentials using the service account file
credentials = service_account.Credentials.from_service_account_file(
Expand All @@ -17,9 +18,8 @@
drive_service = build("drive", "v3", credentials=credentials)


def upload_photo(name, buffer):
def upload_photo(image: Image) -> None:
name: str = f"{datetime.datetime.now().timestamp()}.jpg"
file_metadata = {"name": name, "parents": [PARENT_FOLDER_ID]}

media = MediaIoBaseUpload(io.BytesIO(buffer.tobytes()), mimetype="image/jpeg")

file = drive_service.files().create(body=file_metadata, media_body=media).execute()
media = MediaIoBaseUpload(image.tobytes(), mimetype="image/jpeg")
_ = drive_service.files().create(body=file_metadata, media_body=media).execute()
Loading