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
19 changes: 19 additions & 0 deletions .github/workflows/ruff.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
name: Ruff

on:
push:
paths:
- src/**
pull_request:

permissions:
contents: read

jobs:
ruff:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: chartboost/ruff-action@v1
with:
src: "./src"
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
1 change: 0 additions & 1 deletion Config/config.yaml.dist

This file was deleted.

36 changes: 0 additions & 36 deletions DataCollector/capture_and_send.py

This file was deleted.

25 changes: 0 additions & 25 deletions DataCollector/gdrive.py

This file was deleted.

87 changes: 0 additions & 87 deletions main.py

This file was deleted.

1 change: 1 addition & 0 deletions requirements-dev.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ruff~=0.3.5
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.17.0
PyYAML~=6.0.1
numpy~=1.26.4
1 change: 1 addition & 0 deletions src/config/config.yaml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
api_key: ""
42 changes: 42 additions & 0 deletions src/data_collector/camera_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import cv2
from numpy import ndarray

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() -> ndarray | 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()
if not result:
return None
result, image_encoded = cv2.imencode(".jpg", image)
return image_encoded if result else None
41 changes: 41 additions & 0 deletions src/data_collector/gdrive.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import datetime
from io import BytesIO

from google.oauth2 import service_account
from googleapiclient.discovery import build, Resource
from googleapiclient.http import MediaIoBaseUpload
from numpy import ndarray

# Define the Google Drive API scopes and service account file path
SCOPES = ["https://www.googleapis.com/auth/drive"]
SERVICE_ACCOUNT_FILE = "./datacollector-419114-bdc0fad0e57d.json"
PARENT_FOLDER_ID = "1ne0IuBRl5cK_PeAnpU8PIaaRpVKQ1lFB"
drive_service: Resource | None = None


def init_drive_service() -> bool:
"""
This function initializes the Google Drive service
:return:
"""
global drive_service
try:
credentials = service_account.Credentials.from_service_account_file(
SERVICE_ACCOUNT_FILE, scopes=SCOPES
)
drive_service = build("drive", "v3", credentials=credentials)
return True
except Exception as e:
print(f"An error occurred: {e}")
return False


def upload_photo(image: ndarray) -> None:
"""
This function uploads an image to Google Drive
:param image: The encoded image to upload
"""
name: str = f"{datetime.datetime.now().timestamp()}.jpg"
file_metadata = {"name": name, "parents": [PARENT_FOLDER_ID]}
media = MediaIoBaseUpload(BytesIO(image.tobytes()), mimetype="image/jpeg")
_ = drive_service.files().create(body=file_metadata, media_body=media).execute()
Loading