Friday, May 28, 2021

Audio encryption & decryption using python

                         This blog contains the code for Audio encryption using python





Step 1: Library installation

* pip install cryptography


Step 2: Import modules

from cryptography.fernet import Fernet


#key generation

key=Fernet.generate_key()

print(key)


Step3: Encryption part

fernet=Fernet(key)

with open('key.key','wb') as filekey:

    filekey.write(key)


with open('key.key','rb') as filekey:

    key=filekey.read()

    

with open('open cv project edited.wav','rb') as file:

    originalaudio=file.read()

    

encrypted=fernet.encrypt(originalaudio)


with open('voice encryption.wav','wb') as encrypted_file:

    encrypted_file.write(encrypted)


Step 4: Decryption part

fernet=Fernet(key)


with open('voice encryption.wav','rb') as enc_file:

    encrypted=enc_file.read()

    

decrypted =fernet.decrypt(encrypted)


with open('voice decryption.wav','wb') as dec_file:

    dec_file.write(decrypted)


Thanks for reading this article..!

Friday, May 21, 2021

Vehicle detection project (Open-CV)

Hello python programmers This blog contains the code for the Vehicle detection project using Open-cv library.



Step1: Import the Open-CV library and .xml file.

import cv2

vehiclexml=cv2.CascadeClassifier('vehicle.xml')


Step 2: Create a function for detecting the webcam frame.

def detection(frame):

    vehicle=vehiclexml.detectMultiScale(frame,1.15,4)

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

        cv2.rectangle(frame,(x,y),(x+w,y+h),color=(0,255,0),thickness=2)

        cv2.putText(frame,'vehicle detected.!',(x+w,y+h+20),cv2.FONT_HERSHEY_SIMPLEX,1,(0,255,0),thickness=2)

   return frame


Step 3: Create a function for capturescreen

def capturescreen():

    realtimevideo=cv2.VideoCapture(0)

    while realtimevideo.isOpened():

        ret,frame=realtimevideo.read()

        controlkey=cv2.waitKey(1)

        if ret:

            vehicleframe=detection(frame)

            cv2.imshow('vehicle detection',vehicleframe)

        else:

            break

        if controlkey==ord('q'):

            break

Step 4: Call all the functions

    realtimevideo.release()

    cv2.destroyAllWindows()


capturescreen()


Full code: Vehicle detection with xml file link

Buy me a coffee...!💓


Tuesday, May 18, 2021

Simple Ball game using Pygame


Step 1: Library Installation

* pip install pygame

Step 2 : Mygame():

class Mygame():
    def main(self):
        #inital speed
        Xspeed=10
        Yspeed=10
        livesinit=5

        paddlespeed=30
        points=0
        bgcolor=0,0,0 #black
        size=width,height =500,500

Step 3 : Pygame engine kick-off:

  #initalizing the pygame engine
        pygame.init()
        screen=pygame.display.set_mode(size)


        #creating game objects
        paddle=pygame.image.load('bat.png')
        paddlerect=paddle.get_rect()

        ball=pygame.image.load('ball.png')
        ballrect=ball.get_rect()


        sound=pygame.mixer.Sound('interact sound.wav')
        sound.set_volume(10)

        bg=pygame.image.load('bggame1.jpg')


Step 4: Preparing for game loop:


#arranging the variables for game loop
        paddlerect=paddlerect.move((width/2)-(paddlerect.right/2),height-20)
        ballrect=ballrect.move(width/2,height/2)
        xspeed=Xspeed
        yspeed=Yspeed
        lives=livesinit
        clock=pygame.time.Clock()
        pygame.key.set_repeat(1,30)
        pygame.mouse.set_visible(0)

Step 5 : Game loop:

while True:
            clock.tick(40) #40 fps

            #events
            for event in pygame.event.get():
                if event.type==pygame.QUIT:
                    sys.exit()
                if event.type==pygame.KEYDOWN:
                    if event.key==pygame.K_ESCAPE:
                        sys.exit()
                    if event.key==pygame.K_LEFT:
                        paddlerect=paddlerect.move(-paddlespeed,0)
                        if (paddlerect.left<0):
                            paddlerect.left=0
                    if event.key==pygame.K_RIGHT:
                        paddlerect=paddlerect.move(paddlespeed,0)
                        if(paddlerect.right>width):
                            paddlerect.right=width

Step 6 : Collisions:

#check if paddle hit ball
            if ballrect.bottom>=paddlerect.top and \
               ballrect.bottom<=paddlerect.bottom and \
               ballrect.right>=paddlerect.left and \
               ballrect.left<=paddlerect.right:

                yspeed=-yspeed
                points+=1
                sound.play(0)

                #offset
                offset= ballrect.center[0] - paddlerect.center[0]

                #offset>0 means ball hits the RHS of paddle
                #offset<0 means ball hits the LHS of paddle

                if offset>0:
                    if offset>30:
                        xspeed=7
                    elif offset>23:
                        xspeed=6
                    elif offset>17:
                        xspeed=5
                else:
                    if offset<-30:
                        xspeed=-7
                    elif offset<-23:
                        xspeed=-6
                    elif offset<-17:
                        xspeed=-5

Step 7 : Collisions II

 #move the ball around the screen
            ballrect=ballrect.move(xspeed,yspeed)
            if ballrect.left<0 or ballrect.right>width:
                xspeed=-xspeed
                sound.play(0)
            if ballrect.top<0:
                yspeed=-yspeed
                sound.play(0)

Step 8 : Lives decrement

 #Check the ball has gone past bat 
            if ballrect.top>height:
                lives-=1

                #start a newball
                xspeed=Xspeed
                rand=random.random()
                if random.random()>0.5:
                    xspeed=-xspeed
                yspeed=Yspeed
                ballrect.center=width*random.random(),height/3.5
                #Lives exhausted
                if lives == 0:                    
                    msg = pygame.font.Font(None,70).render("Game Over", True, (0,255,255), bgcolor)
                    msgrect = msg.get_rect()
                    msgrect = msgrect.move(width / 2 - (msgrect.center[0]), height / 3)
                    screen.blit(msg, msgrect)
                    screen.blit(bg,(500,500))
                    pygame.display.flip()

Step 9 : Game restart & quit loop event:

                while True:
                        restart = False
                        for event in pygame.event.get():
                            if event.type == pygame.QUIT:
                                sys.exit()
                            if event.type == pygame.KEYDOWN:
                                if event.key == pygame.K_ESCAPE:
                                sys.exit()
                                if not (event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT):                                    
                                    restart = True      
                        if restart:                   
                            screen.fill(bgcolor)
                            lives = livesinit
                            points = 0
                            break

Step 10 : Update objects & call functions:

 #Update every objects in the game
            screen.fill(bgcolor)
            screen.blit(bg,(0,0))

            scoretext = pygame.font.Font(None,40).render(str(points), True, (0,255,255), bgcolor)
            scoretextrect = scoretext.get_rect()
            scoretextrect = scoretextrect.move(width - scoretextrect.right, 0)
            screen.blit(scoretext, scoretextrect)
            screen.blit(ball, ballrect)
            screen.blit(paddle, paddlerect)
            
            pygame.display.flip()
            

if __name__ == '__main__':
    br = Mygame()
    br.main()



Thanks for reading this article..!





Monday, May 10, 2021

Doge coin price tracker using python

Step 1: Library Installations
 
* pip install bs4
 
* pip install requests
 
 
 

 
 
Step 2: Importing Libraries 
 
 
 import requests
from bs4 import BeautifulSoup as bs
 
 
Step 3: Requesting the URL 
 
url='https://www.coindesk.com/price/bitcoin'
r=requests.get(url)
 
 
Step 4: Extracting the contents in Html form 

soup=bs(r.content,'html.parser')
 
 
Step 5: Extracting the prices  
 
price=soup.find('div',{'class':'price-large'}) 
print("bitcoin:",price.text)


Step 6: Whether its price is  increasing or Decreasing
 
percent=soup.find('span',{'class':'percent-value-text'})
percentage=float(percent.text)
if percentage>0:
    print('bitcoin increased today')
else:
    print('bitcoin decreased today') 


For more clear understandings of code then check out this video..! 
 
(If video not displayed below change into web version )
 
 
 
 
 
 
 
Full code link : Cryptocurrency tracker
 
 
 

Friday, May 7, 2021

Build a website in 12 lines of Python code (Flask frame work)

Step 1:Installing the dependencies

* pip install flask

* pip install pyshorteners

 


Step2: Importing the libraries

from flask import Flask,request,render_template
import pyshorteners 


Step3: Declare a constructor

app=Flask(__name__)

 

Step4: Create your webpage & Write the logic


@app.route('/')
def home(): 

    if request.method == 'GET':
        url=request.args['name']
        s=pyshorteners.Shortener()
        shorter=s.tinyurl.short(url)
        return shorter

return render_template('home.html') 


Step 5: Run the app

 
if __name__=='__main__':
    app.run(debug=True)

 

Step6: Home.html 

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>URL SHORTNER</title>
</head>
<body>
    <h1>*UNDERATED URL SHORTNER*</h1>
    <form action="/" method="GET">
    <input type="text" name="name" placeholder="Enter the URL">
    <input type="submit">
</form>
</body>


For full understanding of code Must watch the video type of content: 

 

 
 
 
 
 Thanks for reading this article..!
 



Buy me a coffee...!💓