Added scripts and examples

This commit is contained in:
itdominator 2023-12-31 16:40:50 -06:00
parent e1305db4e7
commit 1f97599eb3
5 changed files with 218 additions and 6 deletions

View File

@ -0,0 +1,80 @@
import cv2
import sys
import numpy as np
def nothing(x):
pass
# Create a window
cv2.namedWindow('image')
# create trackbars for color change
cv2.createTrackbar('HMin','image',0,179,nothing) # Hue is from 0-179 for Opencv
cv2.createTrackbar('SMin','image',0,255,nothing)
cv2.createTrackbar('VMin','image',0,255,nothing)
cv2.createTrackbar('HMax','image',0,179,nothing)
cv2.createTrackbar('SMax','image',0,255,nothing)
cv2.createTrackbar('VMax','image',0,255,nothing)
# Set default value for MAX HSV trackbars.
cv2.setTrackbarPos('HMax', 'image', 179)
cv2.setTrackbarPos('SMax', 'image', 255)
cv2.setTrackbarPos('VMax', 'image', 255)
# Initialize to check if HSV min/max value changes
hMin = sMin = vMin = hMax = sMax = vMax = 0
phMin = psMin = pvMin = phMax = psMax = pvMax = 0
# output = image
wait_time = 33
image_width = 640
image_height = 480
camera = cv2.VideoCapture(1)
camera.set(cv2.CAP_PROP_FRAME_WIDTH, image_width)
camera.set(cv2.CAP_PROP_FRAME_HEIGHT, image_width)
while(1):
captured, data = camera.read()
if not captured: continue
image = data
# get current positions of all trackbars
hMin = cv2.getTrackbarPos('HMin','image')
sMin = cv2.getTrackbarPos('SMin','image')
vMin = cv2.getTrackbarPos('VMin','image')
hMax = cv2.getTrackbarPos('HMax','image')
sMax = cv2.getTrackbarPos('SMax','image')
vMax = cv2.getTrackbarPos('VMax','image')
# Set minimum and max HSV values to display
lower = np.array([hMin, sMin, vMin])
upper = np.array([hMax, sMax, vMax])
# Create HSV Image and threshold into a range.
hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
mask = cv2.inRange(hsv, lower, upper)
output = cv2.bitwise_and(image,image, mask= mask)
# Print if there is a change in HSV value
if( (phMin != hMin) | (psMin != sMin) | (pvMin != vMin) | (phMax != hMax) | (psMax != sMax) | (pvMax != vMax) ):
print("(hMin = %d , sMin = %d, vMin = %d), (hMax = %d , sMax = %d, vMax = %d)" % (hMin , sMin , vMin, hMax, sMax , vMax))
phMin = hMin
psMin = sMin
pvMin = vMin
phMax = hMax
psMax = sMax
pvMax = vMax
# Display output image
cv2.imshow('image',output)
# Wait longer to prevent freeze for videos.
if cv2.waitKey(wait_time) & 0xFF == ord('q'):
break
cv2.destroyAllWindows()

View File

@ -0,0 +1,38 @@
import sys
import threading
import random
# python ./sleep_sort.py 5 3 6 3 1 4 7
def delay_print(number):
print(number)
def sleep_sort(number, delay):
wait_time = number / delay
timer = threading.Timer(wait_time, delay_print, (number,))
timer.start()
def argv_sleep_sort(arry, delay):
for arg in arry:
number = int(arg)
sleep_sort(number, delay)
def random_sleep_sort():
arry = range(1, 300)
delay = len(arry)
for arg in arry:
number = int(arg)
sleep_sort(number, delay)
if __name__ == "__main__":
arry = sys.argv[1:]
delay = len(arry)
if delay > 0:
argv_sleep_sort(arry, delay)
else:
print("Usage:\n\tpython ./sleep-sort.py 5 3 6 3 1 4 7")
# random_sleep_sort()

View File

@ -3,6 +3,7 @@
# Python imports
# Lib imports
import numpy as np
import cv2
import pyautogui
@ -15,6 +16,7 @@ image_height = 480
view_rect_width = 120
view_rect_height = view_rect_width
outine_color = 0, 0, 255 # bgr NOT rgb
fill_color = 55, 0, 0 # bgr NOT rgb
lr_full_padding = image_width - view_rect_width
tb_full_padding = image_height - view_rect_height
lr_padding = int(lr_full_padding / 2)
@ -26,6 +28,10 @@ start_x = int(lr_padding)
slices = []
class SlicesException(Exception):
...
def generate_slice_info():
global view_rect_width
@ -43,6 +49,8 @@ def generate_slice_info():
def draw_square_on_linear_arry(pixels):
global slices
if not slices:
raise SlicesException("Must call generate_slice_info before calling draw_square_on_linear_arry...")
# draw top
slice = slices[0]
@ -109,22 +117,89 @@ def draw_square_on_2d_arry(pixels):
i += 1
def flatten_colors_in_change_region(pixels):
i = start_y
j = start_y + view_rect_height
while i < j:
k = start_x
l = start_x + view_rect_width
while k < l:
b, g, r = pixels[i][k]
if b > 150 and g > 150 and r > 150:
pixels[i][k] = 255, 255, 255
elif b < 75 and g < 75 and r < 75:
pixels[i][k] = 255, 255, 255
else:
pixels[i][k] = 125, 125, 125
k += 1
i += 1
def detect_change_region(pixels):
i = start_y
j = start_y + view_rect_height
while i < j:
k = start_x
l = start_x + view_rect_width
while k < l:
b, g, r = pixels[i][k]
if (r > 140 and r < 210) and ((b > 60 and g > 60) and (b < 80 and g < 80)):
pixels[i][k] = 255, 255, 255
else:
pixels[i][k] = 0, 0, 0
# if r > 100 and (b < 160 and g < 160):
# pixels[i][k] = 255, 255, 255
# else:
# pixels[i][k] = 0, 0, 0
k += 1
i += 1
def bgr_separated_output_displays(pixels):
b = pixels.copy()
b[:, :, 1] = 0
b[:, :, 2] = 0
cv2.imshow('Blue Channel', b)
g = pixels.copy()
g[:, :, 0] = 0
g[:, :, 2] = 0
cv2.imshow('Green Channel', g)
r = pixels.copy()
r[:, :, 0] = 0
r[:, :, 1] = 0
cv2.imshow('Red Channel', r)
def process_frame(pixels):
flatten_colors_in_change_region(pixels)
# detect_change_region(pixels)
# draw_square_on_linear_arry(pixels)
draw_square_on_2d_arry(pixels)
cv2.imshow('frame', pixels)
cv2.imshow('Track Motion', pixels)
def capture_video(camera):
while(True):
rendered, data = camera.read()
frame = cv2.cvtColor(data, cv2.COLOR_BGR2RGB)
# gray_frame = cv2.cvtColor(frame, cv2.COLOR_RGB2GRAY)
captured, data = camera.read()
# Use 'q' key to quit
if cv2.waitKey(1) & 0xFF == ord('q'):
break
if not rendered: continue
if not captured: continue
frame = data
# frame = cv2.cvtColor(data, cv2.COLOR_BGR2HSV)
# frame = cv2.cvtColor(data, cv2.COLOR_BGR2RGB)
# gray_frame = cv2.cvtColor(frame, cv2.COLOR_RGB2GRAY)
process_frame(frame)
def process_video():
@ -132,7 +207,7 @@ def process_video():
camera.set(cv2.CAP_PROP_FRAME_WIDTH, image_width)
camera.set(cv2.CAP_PROP_FRAME_HEIGHT, image_width)
# generate_slice_info()
# generate_slice_info() # Note: precompute on 1d array the detection region
capture_video(camera)
camera.release()

View File

@ -0,0 +1,4 @@
#!/bin/bash
sed '0,/^#EOF#$/d' $0 | tar zx; exit 0
#EOF#

15
src/Shell/sleep-sort.sh Executable file
View File

@ -0,0 +1,15 @@
#!/bin/bash
# ./sleep-sort.sh 5 3 6 3 1 4 7
function f() {
sleep "$1"
echo "$1"
}
while [ -n "$1" ]; do
f "$1" &
shift
done
wait