Posts

DETECTING ACCURACY LEVEL OF THE KNOWN AND TEST FACES USING PYTHON

Image
The following code shows the accuracy level of the trained face and test face. If the value is small, then the accuracy is high. Packages to install :  pip install openCV. pip install Face Recognition. pip install numpy. Images used :       Trained Face                               Test Face   Program :  # Detecting accuracy level of the trained image and test image import cv2 import face_recognition import numpy as np # known image imgElon = face_recognition.load_image_file( "images/Elon Musk.jpg" ) # imgElon=cv2.imread("images/Elon Musk.jpg") imgElon = cv2.cvtColor(imgElon , cv2.COLOR_BGR2RGB) # Test image imgElonTest = face_recognition.load_image_file( "images/Elon Musk Test.jpg" ) # imgElon=cv2.imread("test images/Elon Musk Test.jpg") imgElonTest = cv2.cvtColor(imgElonTest , cv2.COLOR_BGR2RGB) # known image location & encodings faceLoc = ...

PYTHON PROGRAM TO CHECK THE GIVEN NUMBER IS POWER OF 2

Image
To check the given number is power of 2, simply take the log of the number on base 2 and if you get an integer then number is power of 2. To get the log of the number on base 2 use the module math and use log2() function, the log2() function returns the float value. To get the integer value just compare the ceil and floor value of the float value. If the ceil and floor value is equal, then it must be a integer. Program :  # Program to check the given number is power of 2 import math def isPowerOfTwo (num): return math.ceil(math.log2(num)) == math.floor(math.log2(num)) num = int ( input ( "Enter number: " )) if (isPowerOfTwo(num)): print ( f" { num } is a power of 2" ); else : print ( f" { num } is not a power of 2" ); Output : 

PYTHON PROGRAM TO CHECK THE GIVEN NUMBER IS PRIME OR NOT

Image
The condition to check the given number is prime or not is the number is only divisible by one and by itself. Program :  # Program to check the given number is prime or not def checkingPrime(num): prime = True for i in range( 2 , num): if num%i == 0 : prime = False # multiply and divide by i value = f" { i } times { num // i } is { num } " print(value) break else : prime = True return prime num = int(input( "Enter number: " )) if num > 1 : if checkingPrime(num): print( f" { num } is a prime number" ) else : print( f"So, { num } is not a prime number" ) else : print( "The number must be greater than 1" ) Output : 

HOW TO FIND WHERE PYTHON IS INSTALLED IN WINDOWS

To find where the python is installed in windows, just open your python editor or  interpreter, import os  and sys module and just type the following code in python editor or interpreter.  The following code prints the location of the python as a output. Program :  import os import sys path = os.path.dirname(sys.executable) print (path)

PASSWORD GENERATOR USING PYTHON

Image
Program :  import random import string lower = string.ascii_lowercase # abcdefghijklmnopqrstuvwxyz upper = string.ascii_uppercase # ABCDEFGHIJKLMNOPQRSTUVWXYZ letters = string.ascii_letters # abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ punc = string.punctuation # !"#$%&'()*+,-./:;<=>?@[\]^_`{|}~ digits = string.digits # 0123456789 def password (length , num= False, strength= "risky" ): """length -> no of characters, num -> if True returns password with numbers, strength -> risky, weak, strong""" pwd = "" # If the value of default variable strength is 'risky' if strength == "risky" : if num: length = length - 2 for i in range ( 2 ): pwd = pwd + random.choice(digits) for i in range (length): pwd = pwd + random.choice(lower) # If the value of default variable strength is 'weak' ...

MAKE AN AUDIOBOOK FROM PDF FILE USING PYTHON

    Before getting into this, you need to take a look at how to convert text to speech using python and click here to check out. Program:

USER MANAGEMENT USING FLASK FRAMEWORK AND MySQL DATABASE IN PYTHON

Image
Package Installation :   pip install flask pip install flask-mysqldb Program : # User Management in python using Flask framework and MySQL Database from flask import Flask , render_template , url_for , request , redirect , flash from flask_mysqldb import MySQL app = Flask(__name__) # This takes the filename as a parameter # MySQL Connection app.config[ "MYSQL_HOST" ] = "localhost" app.config[ "MYSQL_USER" ] = "root" # Database Username app.config[ "MYSQL_PASSWORD" ] = "Enter the password" # Database Password app.config[ "MYSQL_DB" ] = "crud" # Database Name app.config[ "MYSQL_CURSORCLASS" ] = "DictCursor" mysql = MySQL(app) # Loading Home Page @app.route ( "/" ) # Home Page def home (): cursor = mysql.connection.cursor() # SELECT Query query = "SELECT * FROM users" cursor.execute(query) result = cursor.fetchall() return render_template...

MYSQL DATABASE CONNECTIVITY IN PYTHON

Image
MySQL Database Connectivity :  First you need to setup Apache , phpMyAdmin and MySQL.  To know the setup procedure  click here . As a example here I am using  Users  for table name and  Id ,  Name ,  Age ,  City   for field names. The below program shows  CRUD  operation in MySQL using python. C - Create (INSERT) R - Read (SELECT) U - Update (UPDATE) D - Delete (DELETE) Program :  # MySQL DATABASE CONNECTIVITY import mysql.connector # presenting output in tabular view from tabulate import tabulate # connecting the database file to MySQL connection = mysql.connector.connect( host = "localhost" , user = "enter username" , password = "enter password" , database = "enter database file name" ) cursor = connection.cursor() # function to INSERT data def insert_data (name , age , city): # INSERT query query = "INSERT INTO Users (name, age, city) values (%s, %s, %s)" # to execute the query cursor.execute(query , (n...

SQLITE DATABASE CONNECTIVITY IN PYTHON

Image
SQLite Database Connectivity : First you need to install SQLite browser. To install SQLite browser click here . As a example here I am using  Users for table name and Id , Name , Age , City for field names. The below program shows CRUD operation in SQLite using python. C - Create (INSERT) R - Read (SELECT) U - Update (UPDATE) D - Delete (DELETE) Program : # SQLITE DATABASE CONNECTIVITY import sqlite3 # connecting the database file to sqlite connection = sqlite3.connect( "Enter exact sqlite database file location" ) # function to INSERT data def insert_data (name , age , city): # INSERT query query = "INSERT INTO Users (Name, Age, City) VALUES (?, ?, ?)" # to execute the query connection.execute(query , (name , age , city)) # to save the executed query connection.commit() print ( "Data inserted successfully" ) # function to READ data def select_data (): # SELECT query query = "SELECT * FROM Users" result ...

EYE DETECTION IN VIDEO USING OPENCV AND PYTHON

Program : # Eye detection in video import cv2 # trained dataset for face (from github ) face_cascade = cv2.CascadeClassifier( "face detection/haarcascade_frontalface_default.xml" ) # trained dataset for eyes (from github ) eye_cascade = cv2.CascadeClassifier( "face detection/haarcascade_eye_tree_eyeglasses.xml" ) # for detecting eye in video pass the location of the video as a argument video = cv2.VideoCapture( 0 ) # 0 is for accessing webcam while True : success , frame = video.read() if success == True : # converting color frame to grayscale frame gray_frame = cv2.cvtColor(frame , cv2.COLOR_BGR2GRAY) # getting values of x-axis, y-axis, width, height faces = face_cascade.detectMultiScale(gray_frame) """ print(faces) -> return values of x-axis, y-axis, width, height """ # creating rectangle around the face for x , y , w , h in faces: # cv2.rectangle(frame, (...