controlling brightness by hand detection

import cv2

import mediapipe as mp

from math import hypot

import screen_brightness_control as sbc

import numpy as np

mpHands = mp.solutions.hands

hands = mpHands.Hands(

static_image_mode=False,

model_complexity=1,

min_detection_confidence=0.75,

min_tracking_confidence=0.75,

max_num_hands=2)

Draw = mp.solutions.drawing_utils

cap = cv2.VideoCapture(0)

while True:

_, frame = cap.read()

frame = cv2.flip(frame, 1)

frameRGB = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)

Process = hands.process(frameRGB)

landmarkList = []

if Process.multi_hand_landmarks:

for handlm in Process.multi_hand_landmarks:

for _id, landmarks in enumerate(handlm.landmark):

height, width, color_channels = frame.shape

x, y = int(landmarks.x*width), int(landmarks.y*height)

landmarkList.append([_id, x, y])

Draw.draw_landmarks(frame, handlm,

mpHands.HAND_CONNECTIONS)

if landmarkList != []:

x_1, y_1 = landmarkList[4][1], landmarkList[4][2]

x_2, y_2 = landmarkList[8][1], landmarkList[8][2]

cv2.circle(frame, (x_1, y_1), 7, (0, 255, 0), cv2.FILLED)

cv2.circle(frame, (x_2, y_2), 7, (0, 255, 0), cv2.FILLED)

cv2.line(frame, (x_1, y_1), (x_2, y_2), (0, 255, 0), 3)

L = hypot(x_2-x_1, y_2-y_1)

b_level = np.interp(L, [15, 220], [0, 100])

sbc.set_brightness(int(b_level))

cv2.imshow('Image', frame)

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

break


Youtube video downloader

import tkinter as tk

from tkinter import *

from pytube import YouTube

from tkinter import messagebox, filedialog

def Widgets():

head_label = Label(root, text="YouTube Video Downloader",

padx=15,

pady=15,

font="SegoeUI 14",

bg="palegreen",

fg="black")

head_label.grid(row=1,

column=1,

pady=10,

padx=5,

columnspan=3)

link_label = Label(root,

text="YouTube link :",

bg="salmon",

pady=5,

padx=5)

link_label.grid(row=2,

column=0,

pady=5,

padx=5)

root.linkText = Entry(root,

width=35,

textvariable=video_Link,

font="Arial 14")

root.linkText.grid(row=2,

column=1,

pady=5,

padx=5,

columnspan=2)

destination_label = Label(root,

text="Destination :",

bg="salmon",

pady=5,

padx=9)

destination_label.grid(row=3,

column=0,

pady=5,

padx=5)

root.destinationText = Entry(root,

width=27,

textvariable=download_Path,

font="Arial 14")

root.destinationText.grid(row=3,

column=1,

pady=5,

padx=5)

browse_B = Button(root,

text="Browse",

command=Browse,

width=10,

bg="bisque",

relief=GROOVE)

browse_B.grid(row=3,

column=2,

pady=1,

padx=1)

Download_B = Button(root,

text="Download Video",

command=Download,

width=20,

bg="thistle1",

pady=10,

padx=15,

relief=GROOVE,

font="Georgia, 13")

Download_B.grid(row=4,

column=1,

pady=20,

padx=20)

def Browse():

download_Directory = filedialog.askdirectory(

initialdir="YOUR DIRECTORY PATH", title="Save Video")

download_Path.set(download_Directory)

def Download():

Youtube_link = video_Link.get()

download_Folder = download_Path.get()

getVideo = YouTube(Youtube_link)

videoStream = getVideo.streams.first()

videoStream.download(download_Folder)

messagebox.showinfo("SUCCESSFULLY",

"DOWNLOADED AND SAVED IN\n"

+ download_Folder)

root = tk.Tk()

root.geometry("520x280")

root.resizable(False, False)

root.title("YouTube Video Downloader")

root.config(background="light blue")

video_Link = StringVar()

download_Path = StringVar()

Widgets()

root.mainloop()


Counting Fingers

import cv2

import mediapipe as mp

cap = cv2.VideoCapture(0)

mp_Hands = mp.solutions.hands

hands = mp_Hands.Hands()

mpdraw = mp.solutions.drawing_utils

finger_Coord = [(8, 6), (12, 10), (16, 14), (20, 18)]

thumb_Coord = (4,2)

while True:

    success, image = cap.read()

    RGB_image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)

    results = hands.process(RGB_image)

    multiLandMarks = results.multi_hand_landmarks

    if multiLandMarks:

        handList = []

        for handLms in multiLandMarks:

            mpdraw.draw_landmarks(image, handLms, mp_Hands.HAND_CONNECTIONS)

            for idx, lm in enumerate(handLms.landmark):

                h, w, c = image.shape

                cx, cy = int(lm.x * w), int(lm.y * h)

                handList.append((cx, cy))

            for point in handList:

                cv2.circle(image, point, 10, (255, 255, 0), cv2.FILLED)

                upCount = 0

        for coordinate in finger_Coord:

            if handList[coordinate[0]][1] < handList[coordinate[1]][1]:

                upCount += 1

        if handList[thumb_Coord[0]][0] > handList[thumb_Coord[1]][0]:

            upCount += 1

            cv2.putText(image, str(upCount), (150,150), cv2.FONT_HERSHEY_PLAIN, 12, (0,255,0), 12)


    cv2.imshow("Counting number of fingers", image)

    cv2.waitKey(1)

sudoku

board=[

    [2,3,0,0,7,0,0,0,0],

    [6,0,0,1,9,5,0,0,0],

    [0,9,8,0,0,0,0,6,0],

    [8,0,0,0,6,0,0,0,3],

    [4,0,0,8,0,3,0,0,1],

    [5,0,0,0,2,0,0,0,6],

    [0,6,0,0,0,0,2,8,0],

    [0,0,0,4,1,9,0,0,5],

    [0,0,0,0,9,0,0,8,9]]

def print_board(bo):

    for i in range(len(bo)):

        if i%3 == 0 and i!=0:

            print("---------------------")

        for j in range(len(bo[0])):

            if j%3 == 0 and j!=0:

                print(" | ", end ="")

            if j==8:

                print(bo[i][j])

            else:

                print(str(bo[i][j]) + " ",end="")

def find_empty(bo):

    for i in range(len(bo)):

        for j in range(len(bo[0])):

            if bo[i][j] == 0:

                return (i, j)

    return None

def valid(bo, num, pos):

    for i in range(len(bo[0])):

        if bo[pos[0]][i] == num and pos[1]!= i:

            return False

    for j in range(len(bo)):

        if bo[i][pos[1]] == num and pos[0]!= i:

            return False

    box_x = pos[1]//3

    box_y = pos[0]//3

    for i in range(box_y*3, box_y*3 + 3):

        for j in range(box_x*3, box_x*3 + 3):

            if bo[i][j] == num and  (i,j) != pos:

                return False

    return True

def solve(bo):

    find = find_empty(bo)

    if not find:

        return True

    else:

        row, col = find

    for i in range(1,10):

        if valid(bo, i , (row,col)):

            bo[row][col] = i

            if solve(bo):

                return True

            bo[row][col]=0

    return False

print_board(board)

solve(board)

print("Solving")

print_board(board)

print("Solved")

snowman

import turtle

turtle.speed(2)

turtle.setup(800, 700)

turtle.bgcolor("black")

turtle.penup()

turtle.goto(0, -320)

turtle.pendown()

turtle.color("#c1f8b5")

turtle.begin_fill()

turtle.circle(320)

turtle.end_fill()

turtle.penup()

turtle.goto(0, -280)

turtle.pendown()

turtle.color("white")

turtle.begin_fill()

turtle.circle(110)

turtle.end_fill()

turtle.penup()

turtle.goto(0, -110)

turtle.pendown()

turtle.begin_fill()

turtle.circle(90)

turtle.end_fill()

turtle.penup()

turtle.goto(0, 20)

turtle.pendown()

turtle.begin_fill()

turtle.circle(70)

turtle.end_fill()

def black_circle():

turtle.color("black")

turtle.begin_fill()

turtle.circle(10)

turtle.end_fill()

x = -20

for i in range(2):

turtle.penup()

turtle.goto(x, 110)

turtle.pendown()

black_circle()

x = x + 40

y = 0

for i in range(5):

turtle.penup()

turtle.goto(0, y)

turtle.pendown()

black_circle()

y = y - 55

turtle.penup()

turtle.goto(0,70)

turtle.pendown()

turtle.color("red")

turtle.begin_fill()

turtle.circle(17)

turtle.end_fill()

turtle.penup()

turtle.goto(0,75)

turtle.pendown()

turtle.color("white")

turtle.begin_fill()

turtle.circle(17)

turtle.end_fill()

turtle.penup()

turtle.goto(75, 0)

turtle.pendown()

turtle.color("brown")

turtle.begin_fill()

turtle.left(40)

for i in range(2):

turtle.forward(75)

turtle.left(90)

turtle.forward(7)

turtle.left(90)

turtle.end_fill()

turtle.penup()

turtle.goto(115, 38)

turtle.pendown()

turtle.begin_fill()

turtle.left(40)

for i in range(2):

turtle.forward(25)

turtle.left(90)

turtle.forward(5)

turtle.left(90)

turtle.end_fill()

turtle.begin_fill()

turtle.right(100)

for i in range(2):

turtle.forward(25)

turtle.left(90)

turtle.forward(5)

turtle.left(90)

turtle.end_fill()

turtle.penup()

turtle.goto(-130, 50)

turtle.pendown()

turtle.begin_fill()

turtle.right(10)

for i in range(2):

turtle.forward(75)

turtle.right(90)

turtle.forward(7)

turtle.right(90)

turtle.end_fill()

turtle.penup()

turtle.goto(-112, 58)

turtle.pendown()

turtle.begin_fill()

turtle.right(40)

for i in range(2):

turtle.forward(25)

turtle.right(90)

turtle.forward(5)

turtle.right(90)

turtle.end_fill()

turtle.begin_fill()

turtle.right(100)

turtle.penup()

turtle.goto(-108, 31)

turtle.pendown()

for i in range(2):

turtle.forward(25)

turtle.right(90)

turtle.forward(5)

turtle.right(90)

turtle.end_fill()

turtle.penup()

turtle.goto(50, 150)

turtle.pendown()

turtle.color("black")

turtle.begin_fill()

turtle.right(10)

turtle.forward(100)

turtle.right(90)

turtle.forward(10)

turtle.right(90)

turtle.forward(20)

turtle.left(90)

turtle.forward(45)

turtle.right(90)

turtle.forward(60)

turtle.right(90)

turtle.forward(45)

turtle.left(90)

turtle.forward(20)

turtle.right(90)

turtle.end_fill()

turtle.hideturtle()