Skip to content
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
76 changes: 40 additions & 36 deletions Camera_Color_Detection_and_Tracking_with_OpenCV.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,15 @@
cap.set(4, frameHeight)

def emptyFunction(a):
threshold1 = cv2.getTrackbarPos("Threshold1", "Parameters")
threshold2 = cv2.getTrackbarPos("Threshold2", "Parameters")
area = cv2.getTrackbarPos("Area", "Parameters")
print("Threshold1:", threshold1)
print("Threshold2:", threshold2)
print("Area:", area)
hMin = cv2.getTrackbarPos("HUE Min", "HSV")
hMax = cv2.getTrackbarPos("HUE Max", "HSV")
sMin = cv2.getTrackbarPos("SAT Min", "HSV")
sMax = cv2.getTrackbarPos("SAT Max", "HSV")
vMin = cv2.getTrackbarPos("VALUE Min", "HSV")
vMax = cv2.getTrackbarPos("VALUE Max", "HSV")
print("HUE Min:", hMin, "HUE Max:", hMax)
print("SAT Min:", sMin, "SAT Max:", sMax)
print("VALUE Min:", vMin, "VALUE Max:", vMax)

cv2.namedWindow("HSV")
cv2.resizeWindow("HSV", 640, 240)
Expand All @@ -24,33 +27,34 @@ def emptyFunction(a):
cv2.createTrackbar("VALUE Min", "HSV", 0, 255, emptyFunction)
cv2.createTrackbar("VALUE Max", "HSV", 255, 255, emptyFunction)

while True:
ret, frame = cap.read()

if not ret:
print("Failed to capture frame from the camera.")
break

imgHsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)

hMin = cv2.getTrackbarPos("HUE Min", "HSV")
hMax = cv2.getTrackbarPos("HUE Max", "HSV")
sMin = cv2.getTrackbarPos("SAT Min", "HSV")
sMax = cv2.getTrackbarPos("SAT Max", "HSV")
vMin = cv2.getTrackbarPos("VALUE Min", "HSV")
vMax = cv2.getTrackbarPos("VALUE Max", "HSV")

lower = np.array([hMin, sMin, vMin])
upper = np.array([hMax, sMax, vMax])
mask = cv2.inRange(imgHsv, lower, upper)
result = cv2.bitwise_and(frame, frame, mask=mask)

mask = cv2.cvtColor(mask, cv2.COLOR_GRAY2BGR)
hStack = np.hstack([frame, mask, result])
cv2.imshow('Horizontal Stacking', hStack)

if cv2.waitKey(1) & 0xFF == ord('q'):
break

cap.release()
cv2.destroyAllWindows()
try:
while True:
ret, frame = cap.read()

if not ret:
print("Failed to capture frame from the camera.")
break

imgHsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)

hMin = cv2.getTrackbarPos("HUE Min", "HSV")
hMax = cv2.getTrackbarPos("HUE Max", "HSV")
sMin = cv2.getTrackbarPos("SAT Min", "HSV")
sMax = cv2.getTrackbarPos("SAT Max", "HSV")
vMin = cv2.getTrackbarPos("VALUE Min", "HSV")
vMax = cv2.getTrackbarPos("VALUE Max", "HSV")

lower = np.array([hMin, sMin, vMin])
upper = np.array([hMax, sMax, vMax])
mask = cv2.inRange(imgHsv, lower, upper)
result = cv2.bitwise_and(frame, frame, mask=mask)

mask = cv2.cvtColor(mask, cv2.COLOR_GRAY2BGR)
hStack = np.hstack([frame, mask, result])
cv2.imshow('Horizontal Stacking', hStack)

if cv2.waitKey(1) & 0xFF == ord('q'):
break
finally:
cap.release()
cv2.destroyAllWindows()
38 changes: 23 additions & 15 deletions Real-time_Object_Detection_and_Tracking.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,21 +70,29 @@ def getContours(img,imgContour):
cv2.putText(imgContour, "Points: " + str(len(approx)), (x + w + 20, y + 20), cv2.FONT_HERSHEY_COMPLEX, .7, (0, 255, 0), 2)
cv2.putText(imgContour, "Area: " + str(int(area)), (x + w + 20, y + 45), cv2.FONT_HERSHEY_COMPLEX, 0.7, (0, 255, 0), 2)

while True:
success, img = cap.read()
imgContour = img.copy()
imgBlur = cv2.GaussianBlur(img, (7, 7), 1)
imgGray = cv2.cvtColor(imgBlur, cv2.COLOR_BGR2GRAY)
threshold1 = cv2.getTrackbarPos("Threshold1", "Parameters")
threshold2 = cv2.getTrackbarPos("Threshold2", "Parameters")
imgCanny = cv2.Canny(imgGray,threshold1,threshold2)
kernel = np.ones((5, 5))
imgDil = cv2.dilate(imgCanny, kernel, iterations=1)
getContours(imgDil,imgContour)
imgStack = stackImages(0.8,([img,imgCanny], [imgDil,imgContour]))
cv2.imshow("Result", imgStack)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
try:
while True:
success, img = cap.read()
if not success:
print("Failed to capture image")
break

imgContour = img.copy()
imgBlur = cv2.GaussianBlur(img, (7, 7), 1)
imgGray = cv2.cvtColor(imgBlur, cv2.COLOR_BGR2GRAY)
threshold1 = cv2.getTrackbarPos("Threshold1", "Parameters")
threshold2 = cv2.getTrackbarPos("Threshold2", "Parameters")
imgCanny = cv2.Canny(imgGray,threshold1,threshold2)
kernel = np.ones((5, 5))
imgDil = cv2.dilate(imgCanny, kernel, iterations=1)
getContours(imgDil,imgContour)
imgStack = stackImages(0.8,([img,imgCanny], [imgDil,imgContour]))
cv2.imshow("Result", imgStack)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
finally:
cap.release()
cv2.destroyAllWindows()



Expand Down
136 changes: 70 additions & 66 deletions Real-time_Object_Tracking.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,75 +3,79 @@
import cv2
import time

# Create a tracker object
tracker = cv2.TrackerKCF_create()
# Create a tracker object (handle OpenCV version compatibility)
try:
tracker = cv2.TrackerKCF_create()
except AttributeError:
tracker = cv2.legacy.TrackerKCF_create()

# Open the video capture
cap = cv2.VideoCapture(0)

# Read the first frame from the video
success, frame = cap.read()
if not success:
print("Failed to read video")
exit()

# Select the region of interest (ROI) for tracking
bbox = cv2.selectROI("Tracking", frame, False)

# Initialize the tracker with the selected ROI
tracker.init(frame, bbox)

# Variables for calculating FPS
fps_start_time = time.time()
fps_counter = 0
fps = 0

# Function to draw bounding box and status on the image
def drawBox(img, bbox):
x, y, w, h = int(bbox[0]), int(bbox[1]), int(bbox[2]), int(bbox[3])
cv2.rectangle(img, (x, y), ((x + w), (y + h)), (0, 255, 255), 3, 3)
cv2.putText(img, "Tracking", (x, y - 10), cv2.FONT_HERSHEY_SCRIPT_SIMPLEX, 0.9, (0, 255, 255), 2, cv2.LINE_AA)

# Main loop for video processing
while True:
# Read a frame from the video
success, img = cap.read()
try:
# Read the first frame from the video
success, frame = cap.read()
if not success:
print("Failed to read video")
break

# Update the tracker with the current frame
success, bbox = tracker.update(img)

# If tracking is successful, draw the bounding box
if success:
drawBox(img, bbox)
else:
# If tracking is lost, display "Lost" message
cv2.putText(img, "Lost", (20, 40), cv2.FONT_HERSHEY_SCRIPT_SIMPLEX, 0.9, (0, 0, 255), 2, cv2.LINE_AA)

# Display status and FPS on the image
cv2.rectangle(img, (15, 15), (200, 90), (255, 0, 0), 2)
cv2.putText(img, "Status:", (20, 75), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (255, 0, 0), 2)
cv2.putText(img, "Tracking", (102, 75), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 255), 2)

# Calculate and display FPS
fps_counter += 1
fps_end_time = time.time() - fps_start_time
if fps_end_time > 1:
fps = fps_counter / fps_end_time
fps_counter = 0
fps_start_time = time.time()

cv2.putText(img, "FPS: {:.2f}".format(fps), (20, 40), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (255, 255, 0), 2)

# Display the image with tracking results
cv2.imshow("Tracking", img)

# Exit the loop if 'q' is pressed
if cv2.waitKey(1) & 0xFF == ord('q'):
break

# Release the video capture and close windows
cap.release()
cv2.destroyAllWindows()
exit()

# Select the region of interest (ROI) for tracking
bbox = cv2.selectROI("Tracking", frame, False)

# Initialize the tracker with the selected ROI
tracker.init(frame, bbox)

# Variables for calculating FPS
fps_start_time = time.time()
fps_counter = 0
fps = 0

# Function to draw bounding box and status on the image
def drawBox(img, bbox):
x, y, w, h = int(bbox[0]), int(bbox[1]), int(bbox[2]), int(bbox[3])
cv2.rectangle(img, (x, y), ((x + w), (y + h)), (0, 255, 255), 3, 3)
cv2.putText(img, "Tracking", (x, y - 10), cv2.FONT_HERSHEY_SCRIPT_SIMPLEX, 0.9, (0, 255, 255), 2, cv2.LINE_AA)

# Main loop for video processing
while True:
# Read a frame from the video
success, img = cap.read()
if not success:
print("Failed to read video")
break

# Update the tracker with the current frame
success, bbox = tracker.update(img)

# If tracking is successful, draw the bounding box
if success:
drawBox(img, bbox)
else:
# If tracking is lost, display "Lost" message
cv2.putText(img, "Lost", (20, 40), cv2.FONT_HERSHEY_SCRIPT_SIMPLEX, 0.9, (0, 0, 255), 2, cv2.LINE_AA)

# Display status and FPS on the image
cv2.rectangle(img, (15, 15), (200, 90), (255, 0, 0), 2)
cv2.putText(img, "Status:", (20, 75), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (255, 0, 0), 2)
cv2.putText(img, "Tracking", (102, 75), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 255), 2)

# Calculate and display FPS
fps_counter += 1
fps_end_time = time.time() - fps_start_time
if fps_end_time > 1:
fps = fps_counter / fps_end_time
fps_counter = 0
fps_start_time = time.time()

cv2.putText(img, "FPS: {:.2f}".format(fps), (20, 40), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (255, 255, 0), 2)

# Display the image with tracking results
cv2.imshow("Tracking", img)

# Exit the loop if 'q' is pressed
if cv2.waitKey(1) & 0xFF == ord('q'):
break
finally:
# Release the video capture and close windows
cap.release()
cv2.destroyAllWindows()
52 changes: 30 additions & 22 deletions Video_Capture.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,8 @@ def stack_images(scale, img_array):
if rows_available:
# Stack images horizontally
hor = [np.hstack(img_row) for img_row in img_array]
# Stack rows vertically```python
ver = np.vstack(hor)
# Stack rows vertically
ver = np.vstack(hor)
else:
# Stack images horizontally
hor = np.hstack(img_array)
Expand All @@ -94,23 +94,31 @@ def stack_images(scale, img_array):
return cv2.resize(ver, None, fx=scale, fy=scale)


while True:
success, img = cap.read()
img = cv2.resize(img, (width_img, height_img))
img_contour = img.copy()

img_thres = preprocess_image(img)
biggest = get_contours(img_thres)

if biggest.size != 0:
img_warped = get_warp(img, biggest)
image_array = ([img_contour, img_warped])
cv2.imshow("Image Warped", img_warped)
else:
image_array = ([img_contour, img])

stacked_images = stack_images(0.6, image_array)
cv2.imshow("Capture Recording", stacked_images)

if cv2.waitKey(1) & 0xFF == ord('q'):
break
try:
while True:
success, img = cap.read()
if not success:
print("Failed to capture image")
break

img = cv2.resize(img, (width_img, height_img))
img_contour = img.copy()

img_thres = preprocess_image(img)
biggest = get_contours(img_thres)

if biggest.size != 0:
img_warped = get_warp(img, biggest)
image_array = ([img_contour, img_warped])
cv2.imshow("Image Warped", img_warped)
else:
image_array = ([img_contour, img])

stacked_images = stack_images(0.6, image_array)
cv2.imshow("Capture Recording", stacked_images)

if cv2.waitKey(1) & 0xFF == ord('q'):
break
finally:
cap.release()
cv2.destroyAllWindows()
25 changes: 13 additions & 12 deletions adjust_camera.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,20 @@

cap = cv2.VideoCapture(0) # Adjust the camera index if needed

while True:
success, frame = cap.read()
try:
while True:
success, frame = cap.read()

if not success:
print("Failed to read frame")
break
if not success:
print("Failed to read frame")
break

print(frame.shape) # Print the shape of the frame (height, width, channels)
print(frame.shape) # Print the shape of the frame (height, width, channels)

cv2.imshow("Frame", frame)
cv2.imshow("Frame", frame)

if cv2.waitKey(1) & 0xFF == ord('q'):
break

cap.release()
cv2.destroyAllWindows()
if cv2.waitKey(1) & 0xFF == ord('q'):
break
finally:
cap.release()
cv2.destroyAllWindows()
Loading