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

added camera2.py #43

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Code/Server/camera.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
from picamera2 import Picamera2
picam2 = Picamera2()
picam2.start_and_capture_file("image.jpg")
picam2.start_and_capture_file("image.jpg")
19 changes: 19 additions & 0 deletions Code/Server/camera2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import cv2

# Create a VideoCapture object
cap = cv2.VideoCapture(0) # Use 0 for the default camera, or specify a different index if you have multiple cameras

while True:
# Read a frame from the camera
ret, frame = cap.read()

# Display the frame in a window
cv2.imshow('Camera', frame)

# Wait for the 'q' key to be pressed to exit the loop
if cv2.waitKey(1) & 0xFF == ord('q'):
break

# Release the VideoCapture object and close the window
cap.release()
cv2.destroyAllWindows()
39 changes: 39 additions & 0 deletions Code/Server/cameraPerks.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import cv2
import cv2.aruco as aruco

# Load the pre-defined dictionary for ArUco codes
aruco_dict = aruco.Dictionary_get(aruco.DICT_4X4_100)

# Create the ArUco parameters
parameters = aruco.DetectorParameters_create()

# Create a VideoCapture object
cap = cv2.VideoCapture(0) # Use 0 for the default camera, or specify a different index if you have multiple cameras

while True:
# Read a frame from the camera
ret, frame = cap.read()

# Convert the frame to grayscale
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

# Detect ArUco markers in the grayscale frame
corners, ids, rejected = aruco.detectMarkers(gray, aruco_dict, parameters=parameters)

# Draw detected ArUco markers and their IDs
if ids is not None:
aruco.drawDetectedMarkers(frame, corners, ids)
for i in range(len(ids)):
cv2.putText(frame, str(ids[i][0]), (corners[i][0][0][0], corners[i][0][0][1] - 5),
cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 255), 2, cv2.LINE_AA)

# Display the frame in a window
cv2.imshow('Camera', frame)

# Wait for the 'q' key to be pressed to exit the loop
if cv2.waitKey(1) & 0xFF == ord('q'):
break

# Release the VideoCapture object and close the window
cap.release()
cv2.destroyAllWindows()
57 changes: 57 additions & 0 deletions Code/Server/cameraROS.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import rospy
from sensor_msgs.msg import Image
from cv_bridge import CvBridge
import cv2
import cv2.aruco as aruco

# ROS initialization
rospy.init_node('camera_node')
bridge = CvBridge()

# Load the pre-trained face cascade classifier
face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')

# Load the pre-defined dictionary for ArUco codes
aruco_dict = aruco.Dictionary_get(aruco.DICT_4X4_100)

# Create the ArUco parameters
parameters = aruco.DetectorParameters_create()

# Callback function to process the camera image
def process_image(msg):
# Convert ROS Image message to OpenCV image
frame = bridge.imgmsg_to_cv2(msg, desired_encoding='bgr8')

# Convert the frame to grayscale for face detection
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

# Detect faces in the grayscale frame
faces = face_cascade.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=5, minSize=(30, 30))

# Draw rectangles around the detected faces
for (x, y, w, h) in faces:
cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 3)

# Detect ArUco markers in the grayscale frame
corners, ids, rejected = aruco.detectMarkers(gray, aruco_dict, parameters=parameters)

# Draw detected ArUco markers and their IDs
if ids is not None:
aruco.drawDetectedMarkers(frame, corners, ids)
for i in range(len(ids)):
cv2.putText(frame, str(ids[i][0]), (corners[i][0][0][0], corners[i][0][0][1] - 5),
cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 255), 2, cv2.LINE_AA)

# Display the frame
cv2.imshow('Camera', frame)
cv2.waitKey(1)

# Subscribe to the camera topic
rospy.Subscriber('/camera_topic', Image, process_image)

# Spin ROS
rospy.spin()

# Close OpenCV windows
cv2.destroyAllWindows()