Skip to content

Instantly share code, notes, and snippets.

@Ari24-cb24
Created October 7, 2024 10:57
Show Gist options
  • Save Ari24-cb24/eb1be24a7f6c8d69d695a716ffca4d66 to your computer and use it in GitHub Desktop.
Save Ari24-cb24/eb1be24a7f6c8d69d695a716ffca4d66 to your computer and use it in GitHub Desktop.
Cam for school
import cv2
import time
import keyboard
import random
import threading
import pygame
BLOCKSIZE = 4
STOP_VID = False
def pg():
global STOP_VID
screen = pygame.display.set_mode((400, 400))
run = True
while run:
screen.fill((0, 0, 255))
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
elif event.type == pygame.MOUSEBUTTONDOWN:
if event.button == 1:
x, y = pygame.mouse.get_pos()
if 0 <= x <= 200 and 0 <= y <= 200:
STOP_VID = not STOP_VID
pygame.draw.rect(screen, (0, 255, 0) if STOP_VID else (255, 0, 0), (0, 0, 200, 200))
pygame.display.update()
pygame.quit()
def check_keypress():
global STOP_VID
while True:
if keyboard.is_pressed('space'):
STOP_VID = not STOP_VID
print("Stopping:", STOP_VID)
if keyboard.is_pressed('esc'):
break
def change_resolution(cam, img):
global BLOCKSIZE
height, width = img.shape[:2]
if random.random() >= .7:
BLOCKSIZE = random.randint(4,5)
for y in range(height // BLOCKSIZE):
for x in range(width // BLOCKSIZE):
startY, startX = y * BLOCKSIZE, x * BLOCKSIZE
colors = []
for dx in range(BLOCKSIZE):
for dy in range(BLOCKSIZE):
b, g, r = img[startY+dy,startX+dx]
colors.append((r, g, b))
ave_rgb = [0, 0, 0]
for color in colors:
ave_rgb[0] += color[0]
ave_rgb[1] += color[1]
ave_rgb[2] += color[2]
ave_rgb[0] //= len(colors)
ave_rgb[1] //= len(colors)
ave_rgb[2] //= len(colors)
img[startY:startY+BLOCKSIZE,startX:startX+BLOCKSIZE] = (ave_rgb[2], ave_rgb[1], ave_rgb[0])
return img
def show_webcam(mirror=False):
global STOP_VID
cam = cv2.VideoCapture(0)
_, old_vid = cam.read()
run = True
while run:
ret_val, img = cam.read()
if len(img) == 0 or not ret_val:
continue
if mirror:
img = cv2.flip(img, 1)
img = change_resolution(cam, img)
time.sleep(random.randint(1, 10)/17)
# [n for n in range(10**6)]
if STOP_VID:
cv2.imshow('Webcam-0xf3Winx7f', old_vid)
else:
cv2.imshow('Webcam-0xf3Winx7f', img)
old_vid = img
if cv2.waitKey(1) == 27:
run = False
break
cv2.destroyAllWindows()
def main():
t = threading.Thread(target=pg)
t.start()
show_webcam(mirror=True)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment