Monday, July 26, 2021

How to convert local video file to text transcript

 SpeechRecognition package supports may APIs like

recognize_bing()
recognize_google()
recognize_google_cloud()
recognize_houndify()
recognize_ibm()
recognize_sphinx()
recognize_wit()

But the only free one with is recognize_google()


#pip3 install SpeechRecognition moviepy
import speech_recognition as sr 
import moviepy.editor as mp


clip = mp.VideoFileClip(r"c:/images/intro1.mp4") 
 
clip.audio.write_audiofile(r"c:/images/converted.wav")

r = sr.Recognizer()
r.energy_threshold = 300   #detect silent

audio = sr.AudioFile("c:/images/converted.wav")

with audio as source:
  r.adjust_for_ambient_noise(source, duration=0.5) #remove background noise
  audio_file = r.record(source)

"""
recognize_bing(): Microsoft Bing Speech
recognize_google(): Google Web Speech API, free, working with audio files under 5 minutes,limited to only 50 requests per day
recognize_google_cloud(): Google Cloud Speech - requires installation of the google-cloud-speech package
recognize_houndify(): Houndify by SoundHound
recognize_ibm(): IBM Speech to Text
recognize_sphinx(): CMU Sphinx - requires installing PocketSphinx
recognize_wit(): Wit.ai
"""

result = r.recognize_google(audio_file)
print(result)


If you want to use the commercial google API, just download google json credentials and use the next code


#pip3 install SpeechRecognition moviepy
#pip3 install --upgrade google-cloud-speech
import speech_recognition as sr 
import moviepy.editor as mp
import os

def GoogleConnection():
    dir = os.path.dirname(__file__)
    GOOGLE_APPLICATION_CREDENTIALS_file = os.path.join(dir, 'xxxxxx.json')
    os.environ["GOOGLE_APPLICATION_CREDENTIALS"]=GOOGLE_APPLICATION_CREDENTIALS_file


clip = mp.VideoFileClip(r"c:/images/intro1.mp4") 
 
clip.audio.write_audiofile(r"c:/images/converted.wav")

r = sr.Recognizer()
r.energy_threshold = 300   #detect silent

audio = sr.AudioFile("c:/images/converted.wav")

with audio as source:
  r.adjust_for_ambient_noise(source, duration=0.5) #remove background noise
  audio_file = r.record(source)


GoogleConnection()
result = r.recognize_google_cloud(audio_file)
print(result)


  

and if we have a sound file ready on google firebase storage, we can process it using the next code


# Imports the Google Cloud client library
from google.cloud import speech
import os

def GoogleConnection():
    dir = os.path.dirname(__file__)
    GOOGLE_APPLICATION_CREDENTIALS_file = os.path.join(dir, 'xxxx.json')
    os.environ["GOOGLE_APPLICATION_CREDENTIALS"]=GOOGLE_APPLICATION_CREDENTIALS_file

GoogleConnection()
# Instantiates a client
client = speech.SpeechClient()

# The name of the audio file to transcribe
gcs_uri = "gs://xxx.appspot.com/Video/converted.wav"

audio = speech.RecognitionAudio(uri=gcs_uri)

config = speech.RecognitionConfig(
    encoding=speech.RecognitionConfig.AudioEncoding.LINEAR16,
    #sample_rate_hertz=44100,
    audio_channel_count=2,
    language_code="en-US",
)

# Detects speech in the audio file
response = client.recognize(config=config, audio=audio)

for result in response.results:
    print("Transcript: {}".format(result.alternatives[0].transcript))





Friday, July 9, 2021

Create HTML button with slop using CSS

Lets go!







 <style>  
.content {position: fixed; top: 50;width: 100%;padding: 20px;}
 .outer {position: relative;height: 50px;width: 250px;text-align: center;line-height: 50px;color: white;font-family: Avenir-heavy; font-size: 20px; cursor: pointer;}  
 .outer:before,.outer:after {position: absolute; content: ''; top: 0px;height: 100%; width: 55%; background: none rgb(72, 216, 227); z-index: -1;}  
 .outer:before {left: 0px;border-radius: 0px;border-right: none;transform-origin: top left;border-top-left-radius: 25px;}  
 .outer:after {right: 0px;border-bottom-right-radius: 25px;border-left: none;}  
 </style>  
 <div class="content" id='content'>  
  <center><div class='outer' onclick="LetsGo()">Lets go!</div></center>  
 </div>  

Play Stop button using CSS only


 <style>  
   .loader {display: block; justify-content: center; align-items: center; margin: 0;position: absolute; top: 40%; left: 48%;}  
  #play-button-container , #pause-button-container { display: inline-block; width: 50px; height: 50px; cursor: pointer; opacity: 0.5; }  
  .play-button , .pause-button { display: inline-block; position: absolute; width: 50px; height: 50px; background: #fff; }  
  .play-button-before { clip-path: polygon(0 0, 50% 25%, 50% 75%, 0% 100%); }  
  .play-button-after {clip-path: polygon(50% 25%, 100% 50%, 100% 50%, 50% 75%); }  
  #pause-button-container .pause-button-before { clip-path: polygon(0 0, 30% 0, 30% 100%, 0% 100%); }  
  #pause-button-container .pause-button-after{ clip-path: polygon(70% 0, 100% 0, 100% 100%, 70% 100%) }   
 </style>  
 <div class="loader">  
  <span id="play-button-container" onclick="this.display='none';document.getElementById("pause-button-container").style.display='';">  
   <span class="play-button play-button-before"></span>  
   <span class="play-button play-button-after"></span>  
  </span>  
  <span id="pause-button-container" onclick="this.display='none';document.getElementById("play-button-container").style.display='';">  
   <span class="pause-button pause-button-before"></span>  
   <span class="pause-button pause-button-after"></span>  
  </span>  
 </div>  

Friday, July 2, 2021

Using Python, how to extract the biggest face from internet image and push this face to firebase storage

 


import firebase_admin
from firebase_admin import credentials
from firebase_admin import storage
def FirebaseStorage():
    dir = os.path.dirname(__file__)
    GOOGLE_APPLICATION_CREDENTIALS_file = os.path.join(dir, 'CREDENTIALS.json')
    print(GOOGLE_APPLICATION_CREDENTIALS_file)
    cred = credentials.Certificate(GOOGLE_APPLICATION_CREDENTIALS_file)
    firebase_admin.initialize_app(cred, {'storageBucket': 'xxxxBucketNamexxxxx.appspot.com' })
    return storage.bucket()

from datetime import datetime
def upload_2_FirebaseStorage(img_bytes):
    now = datetime.now()
    blob = FirebaseStorage().blob('profileIMG/'+now.strftime("%Y%m%d%H%M%S%f.jpg"))
	#use this for local image
    #blob.upload_from_filename(local_blob_path)
    blob.upload_from_string(img_bytes,content_type='image/jpeg')

    blob.make_public()
    return blob.public_url

import cv2
import urllib
import sys
import numpy as np
def ExtractUserIMG(IMG_URL):
    resp = urllib.request.urlopen(IMG_URL)
    image = np.asarray(bytearray(resp.read()), dtype="uint8")
    image = cv2.imdecode(image, cv2.IMREAD_COLOR)
    #Use this for local images
    #imagePath = IMG_PATH
    #image = cv2.imread(imagePath)
    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

    faceCascade = cv2.CascadeClassifier(cv2.data.haarcascades + "haarcascade_frontalface_default.xml")
    faces = faceCascade.detectMultiScale(
        gray,
        scaleFactor=1.3,
        minNeighbors=3,
        minSize=(30, 30)
    )

    print("[INFO] Found {0} Faces.".format(len(faces)))
    
    max_area=0
    area=0
    for (x, y, w, h) in faces:
        area=h*w
        if area>max_area:
            max_area=area
            roi_color = image[y:y + h, x:x + w]
    print("[INFO] Object found. Saving locally.")
    #To save locally
    #cv2.imwrite('c:/images/Big_face.jpg', roi_color)
    img_bytes = cv2.imencode('.jpg', roi_color)[1].tobytes()
    return upload_2_FirebaseStorage(img_bytes) #image path on Firebase


ExtractUserIMG('https://www.quickanddirtytips.com/sites/default/files/styles/convert_webp_article_main_image/public/images/2332/people-persons-peoples.jpg')




Selected image by python script