BGR color palette

import cv2

import numpy as np

def emptyFunction():

pass

def main():

image = np.zeros((512, 512, 3), np.uint8)

windowName ="Open CV Color Palette"

cv2.namedWindow(windowName)

cv2.createTrackbar('Blue', windowName, 0, 255, emptyFunction)

cv2.createTrackbar('Green', windowName, 0, 255, emptyFunction)

cv2.createTrackbar('Red', windowName, 0, 255, emptyFunction)

while(True):

cv2.imshow(windowName, image)

if cv2.waitKey(1) == 27:

break

blue = cv2.getTrackbarPos('Blue', windowName)

green = cv2.getTrackbarPos('Green', windowName)

red = cv2.getTrackbarPos('Red', windowName)

image[:] = [blue, green, red]

print(blue, green, red)

cv2.destroyAllWindows()

if __name__=="__main__":

main()


Countdown Timer

import time

from tkinter import *

from tkinter import messagebox

root = Tk()

root.geometry("300x250")

root.title("Time Counter")

hour=StringVar()

minute=StringVar()

second=StringVar()

hour.set("00")

minute.set("00")

second.set("00")

hourEntry= Entry(root, width=3, font=("Arial",18,""),

textvariable=hour)

hourEntry.place(x=80,y=20)


minuteEntry= Entry(root, width=3, font=("Arial",18,""),

textvariable=minute)

minuteEntry.place(x=130,y=20)


secondEntry= Entry(root, width=3, font=("Arial",18,""),

textvariable=second)

secondEntry.place(x=180,y=20)

def submit():

try:

temp = int(hour.get())*3600 + int(minute.get())*60 + int(second.get())

except:

print("Please input the right value")

while temp >-1:

mins,secs = divmod(temp,60)

hours=0

if mins >60:

hours, mins = divmod(mins, 60)

hour.set("{0:2d}".format(hours))

minute.set("{0:2d}".format(mins))

second.set("{0:2d}".format(secs))

root.update()

time.sleep(1)

if (temp == 0):

messagebox.showinfo("Time Countdown", "Time's up ")

temp -= 1

btn = Button(root, text='Set Time Countdown', bd='5',

command= submit)

btn.place(x = 70,y = 120)

root.mainloop()


Pedestrian Detector for images

import cv2

import imutils

# detector

hog = cv2.HOGDescriptor()

hog.setSVMDetector(cv2.HOGDescriptor_getDefaultPeopleDetector())

# Reading the Image

image = cv2.imread('img_path')

# Resizing the Image

image = imutils.resize(image,

width=min(400, image.shape[1]))

# Detecting all the regions in the

# Image that has a pedestrians inside it

(regions, _) = hog.detectMultiScale(image,

winStride=(4, 4),

padding=(4, 4),

scale=1.05)

# Drawing the regions in the Image

for (x, y, w, h) in regions:

cv2.rectangle(image, (x, y),

(x + w, y + h),

(0, 0, 255), 2)

# Showing the output Image

cv2.imshow("Image", image)

cv2.waitKey(0)

cv2.destroyAllWindows()


Multiple color live camera

import cv2

import numpy as np

# capturing first camera 

cap = cv2.VideoCapture(0)

while True:

ret, frame = cap.read()

# Capturing grayscale

gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

cv2.imshow('frame', frame)

cv2.imshow('gray', gray)

# Program will terminate when 'q' key is pressed

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

break

# Releasing resources

cap.release()

cv2.destroyAllWindows()


weight converter

from tkinter import *

window = Tk()

def from_kg():

gram = float(e2_value.get())*1000

pound = float(e2_value.get())*2.20462

ounce = float(e2_value.get())*35.274

t1.delete("1.0", END)

t1.insert(END,gram)

t2.delete("1.0", END)

t2.insert(END,pound)

t3.delete("1.0", END)

t3.insert(END,ounce)

e1 = Label(window, text = "Enter the weight in Kg")

e2_value = StringVar()

e2 = Entry(window, textvariable = e2_value)

e3 = Label(window, text = 'Gram')

e4 = Label(window, text = 'Pounds')

e5 = Label(window, text = 'Ounce')

t1 = Text(window, height = 1, width = 20)

t2 = Text(window, height = 1, width = 20)

t3 = Text(window, height = 1, width = 20)

b1 = Button(window, text = "Convert", command = from_kg)

e1.grid(row = 0, column = 0)

e2.grid(row = 0, column = 1)

e3.grid(row = 1, column = 0)

e4.grid(row = 1, column = 1)

e5.grid(row = 1, column = 2)

t1.grid(row = 2, column = 0)

t2.grid(row = 2, column = 1)

t3.grid(row = 2, column = 2)

b1.grid(row = 0, column = 2)

window.mainloop()