Skip to content

Commit

Permalink
Fixing flake8 issues
Browse files Browse the repository at this point in the history
Note that due to code duplication, the fixes have to be applied to
both the main.py and tracker.py.

Addresses #29
  • Loading branch information
gshiva committed Dec 11, 2023
1 parent f01d66a commit 5e8fc05
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 31 deletions.
3 changes: 3 additions & 0 deletions .flake8
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[flake8]
max-line-length = 120

42 changes: 27 additions & 15 deletions src/pygptcourse/main.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
#!/usr/bin/python

import cv2

import face_recognition # type: ignore

from pygptcourse.tshirt_launcher import Launcher
from pygptcourse.tshirt_launcher import RIGHT, LEFT, STOP, FIRE, DOWN, UP
from pygptcourse.tshirt_launcher import DOWN, LEFT, RIGHT, STOP, UP, Launcher

shiva_image = face_recognition.load_image_file("shiva_face.jpg")
shiva_face_encoding = face_recognition.face_encodings(shiva_image)[0]
Expand Down Expand Up @@ -68,31 +66,41 @@ def move_camera(direction, duration):
# Make sure the current position is within the image bounds
# Explanation from ChatGPT
# These lines of code ensure that the current position of the camera is within the bounds of the image.
# The current_camera_position variable is a list that stores the x and y coordinates of the current position of the camera.
# The first line of code sets the x coordinate of current_camera_position to be within the range [0, TOTAL_TIME_LR],
# The current_camera_position variable is a list that stores the x and y coordinates of the current
# position of the camera.
# The first line of code sets the x coordinate of current_camera_position to be within the
# range [0, TOTAL_TIME_LR],
# and the second line sets the y coordinate to be within the range [0, TOTAL_TIME_LR].
# The max and min functions are used to achieve this.
# The min function takes two arguments and returns the smaller of the two.
# In this case, it is used to ensure that the x and y coordinates of current_camera_position
# do not exceed TOTAL_TIME_LR and TOTAL_TIME_LR, respectively.
# The max function takes two arguments and returns the larger of the two.
# In this case, it is used to ensure that the x and y coordinates of current_camera_position do not fall below 0.
# In this case, it is used to ensure that the x and y coordinates of current_camera_position
# do not fall below 0.
# For example, if current_camera_position[0] is greater than TOTAL_TIME_LR,
# then min(current_camera_position[0], TOTAL_TIME_LR) will return TOTAL_TIME_LR, ensuring that the x coordinate of current_camera_position
# does not exceed the camera left to right limits. Similarly, if current_camera_position[1] is less than 0, then
# max(0, current_camera_position[1]) will return 0, ensuring that the y coordinate of current_camera_position does not fall below 0.
# then min(current_camera_position[0], TOTAL_TIME_LR) will return TOTAL_TIME_LR, ensuring that
# the x coordinate of current_camera_position
# does not exceed the camera left to right limits. Similarly, if current_camera_position[1] is less
# than 0, then
# max(0, current_camera_position[1]) will return 0, ensuring that the y coordinate of
# current_camera_position does not fall below 0.

current_camera_position[0] = max(0, min(current_camera_position[0], TOTAL_TIME_LR))
current_camera_position[1] = max(0, min(current_camera_position[1], TOTAL_TIME_TB))

print(
f"Previous position: {prev_current_camera_position} Calculated current position: {current_camera_position}, Direction: {direction}, Duration: {duration}"
f"Previous position: {prev_current_camera_position} "
f"Calculated current position: {current_camera_position}, "
f"Direction: {direction}, "
f"Duration: {duration}"
)

if prev_current_camera_position == current_camera_position:
# nothing to do
print(
f"Nothing to do. Current position: {current_camera_position} is same as previous position {prev_current_camera_position}."
f"Nothing to do. Current position: {current_camera_position} "
f"is same as previous position {prev_current_camera_position}."
)
return
print(
Expand Down Expand Up @@ -196,10 +204,14 @@ def move_camera_to_center():
face_bbox = [top, right, bottom, left]

# Calculate the center of the face bounding box
# The face_recognition library returns the bounding box of a face in an image as a tuple of four values (top, right, bottom, left).
# The following code calculates the center of the bounding box by taking the average of the left and right values and
# the average of the top and bottom values. This is done by adding the right and left values and dividing by 2
# to get the x-coordinate of the center, and adding the top and bottom values and dividing by 2 to get the y-coordinate
# The face_recognition library returns the bounding box of a face in an image as a tuple of
# four values (top, right, bottom, left).
# The following code calculates the center of the bounding box by taking the average of the
# left and right values and
# the average of the top and bottom values. This is done by adding the
# right and left values and dividing by 2
# to get the x-coordinate of the center, and adding the top and bottom values
# and dividing by 2 to get the y-coordinate
# of the center. The result is a list containing the x and y coordinates of the center of the bounding box.

face_center = [
Expand Down
43 changes: 29 additions & 14 deletions src/pygptcourse/tracker.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def detect_faces():
face_encodings = []

ret, image = video_capture.read()
print(f"Video captured")
print("Video captured")

if not ret:
return []
Expand Down Expand Up @@ -148,31 +148,41 @@ def move_camera(direction, duration):
# Make sure the current position is within the image bounds
# Explanation from ChatGPT
# These lines of code ensure that the current position of the camera is within the bounds of the image.
# The current_camera_position variable is a list that stores the x and y coordinates of the current position of the camera.
# The first line of code sets the x coordinate of current_camera_position to be within the range [0, TOTAL_TIME_LR],
# The current_camera_position variable is a list that stores the x and y coordinates of the current
# position of the camera.
# The first line of code sets the x coordinate of current_camera_position to be within the
# range [0, TOTAL_TIME_LR],
# and the second line sets the y coordinate to be within the range [0, TOTAL_TIME_LR].
# The max and min functions are used to achieve this.
# The min function takes two arguments and returns the smaller of the two.
# In this case, it is used to ensure that the x and y coordinates of current_camera_position
# do not exceed TOTAL_TIME_LR and TOTAL_TIME_LR, respectively.
# The max function takes two arguments and returns the larger of the two.
# In this case, it is used to ensure that the x and y coordinates of current_camera_position do not fall below 0.
# In this case, it is used to ensure that the x and y coordinates of current_camera_position
# do not fall below 0.
# For example, if current_camera_position[0] is greater than TOTAL_TIME_LR,
# then min(current_camera_position[0], TOTAL_TIME_LR) will return TOTAL_TIME_LR, ensuring that the x coordinate of current_camera_position
# does not exceed the camera left to right limits. Similarly, if current_camera_position[1] is less than 0, then
# max(0, current_camera_position[1]) will return 0, ensuring that the y coordinate of current_camera_position does not fall below 0.
# then min(current_camera_position[0], TOTAL_TIME_LR) will return TOTAL_TIME_LR, ensuring that
# the x coordinate of current_camera_position
# does not exceed the camera left to right limits. Similarly, if current_camera_position[1] is less
# than 0, then
# max(0, current_camera_position[1]) will return 0, ensuring that the y coordinate of
# current_camera_position does not fall below 0.

current_camera_position[0] = max(0, min(current_camera_position[0], TOTAL_TIME_LR))
current_camera_position[1] = max(0, min(current_camera_position[1], TOTAL_TIME_TB))

print(
f"Previous position: {prev_current_camera_position} Calculated current position: {current_camera_position}, Direction: {direction}, Duration: {duration}"
f"Previous position: {prev_current_camera_position} "
f"Calculated current position: {current_camera_position}, "
f"Direction: {direction}, "
f"Duration: {duration}"
)

if prev_current_camera_position == current_camera_position:
# nothing to do
print(
f"Nothing to do. Current position: {current_camera_position} is same as previous position {prev_current_camera_position}."
f"Nothing to do. Current position: {current_camera_position} "
f"is same as previous position {prev_current_camera_position}."
)
return
print(
Expand All @@ -184,18 +194,23 @@ def move_camera(direction, duration):
def track_face():
global current_camera_position

# Detect faces in the frame using OpenCV (this code is just an example and needs to be updated with your own face detection code)
# Detect faces in the frame using OpenCV (this code is just an example
# and needs to be updated with your own face detection code)
faces = detect_faces()

# If a face was detected, track it using the track_face function
if faces and len(faces) > 0:
face_bbox = faces[0]

# Calculate the center of the face bounding box
# The face_recognition library returns the bounding box of a face in an image as a tuple of four values (top, right, bottom, left).
# The following code calculates the center of the bounding box by taking the average of the left and right values and
# the average of the top and bottom values. This is done by adding the right and left values and dividing by 2
# to get the x-coordinate of the center, and adding the top and bottom values and dividing by 2 to get the y-coordinate
# The face_recognition library returns the bounding box of a face in an image as a tuple of
# four values (top, right, bottom, left).
# The following code calculates the center of the bounding box by taking the average of the
# left and right values and
# the average of the top and bottom values. This is done by adding the
# right and left values and dividing by 2
# to get the x-coordinate of the center, and adding the top and bottom values
# and dividing by 2 to get the y-coordinate
# of the center. The result is a list containing the x and y coordinates of the center of the bounding box.

face_center = [
Expand Down
4 changes: 2 additions & 2 deletions src/pygptcourse/tshirt_launcher.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# original source from https://github.com/hovren/pymissile
#!/usr/bin/env python3
#!/usr/bin/env python3 # noqa
# encoding: utf8
import time
import threading
import time

import usb.core # type: ignore
import usb.util # type: ignore
Expand Down

0 comments on commit 5e8fc05

Please sign in to comment.