Friday, 29 May 2020
Saturday, 6 July 2019
Video to Image Converter using OpenCV Python
import cv2
import os
def video_to_frames(video, path_output_dir):
# extract frames from a video and save to directory as 'x.png' where
# x is the frame index
vidcap = cv2.VideoCapture(video)
count = 0
while vidcap.isOpened():
success, image = vidcap.read()
if success:
cv2.imwrite(os.path.join(path_output_dir, '%d.png') % count, image)
count += 1
else:
break
cv2.destroyAllWindows()
vidcap.release()
video_to_frames('VID_20190706_222120.mp4', './out')
OutPut Images
Sunday, 2 December 2018
Dynamic directory read and pickle example in python
import math
from sklearn import neighbors
import os
import os.path
import pickle
import glob
from PIL import Image, ImageDraw
nameL = []
name_indx = []
i = 0
X = []
y = []
dirName = glob.glob('data\\*\\')
for name in dirName:
#print(glob.glob(name+'*.jpg'))
v = os.path.dirname(name)
print(v)
nameL.append(v)
name_indx.append(i)
i = i+1
imgs = glob.glob(name+'*.jpg')
for img_path in imgs:
print(img_path)
X.append(img_path)
print(nameL)
print(name_indx)
dictionary = dict(zip(name_indx, nameL))
print(dictionary)
#pickling.........
pickle_out = open("dict.pickle","wb")
pickle.dump(dictionary, pickle_out)
pickle_out.close()
#Unpickling.......
pickle_in = open("dict.pickle","rb")
example_dict = pickle.load(pickle_in)
print(example_dict)
print(example_dict[1])
Tuesday, 6 November 2018
Extract text from image using Pytesseract in windows platform
For windows Os, we need an installation. Pytesseract binary is available here. Then Add a new variable with name tesseract in environment variables with value C:\Program Files (x86)\Tesseract-OCR\tesseract.exe.
Some cases we need the following line of code (if the environment variable is not added correctly)
Then we need to install a python package: pip install tesseract
Some cases we need the following line of code (if the environment variable is not added correctly)
pytesseract.pytesseract.tesseract_cmd = r'C:\Program Files (x86)\Tesseract-OCR\tesseract.exe'
Here we provide the Pytesseract path to the interpreter.
Full Code
'''
download and install-https://github.com/UB-Mannheim/tesseract/wiki
'''
import numpy as np
import cv2
import time
import pytesseract
pytesseract.pytesseract.tesseract_cmd = r'C:\Program Files (x86)\Tesseract-OCR\tesseract.exe'
frame1 = cv2.imread('poc.jpg',0);
cv2.imwrite('ocr.jpg',frame1)
#from tesseract import image_to_string
text = pytesseract.image_to_string(frame1)
print(text)
cv2.imshow(text,frame1 )
cv2.waitKey(0)
cv2.destroyAllWindows()
Input Image
Output
Saturday, 3 November 2018
ORB Feature matching Example in OpenCv
import cv2
import numpy as np
img1 = cv2.imread("face1.jpg", cv2.IMREAD_GRAYSCALE)
img2 = cv2.imread("face2.jpg", cv2.IMREAD_GRAYSCALE)
# ORB Detector
orb = cv2.ORB_create()
kp1, des1 = orb.detectAndCompute(img1, None)
kp2, des2 = orb.detectAndCompute(img2, None)
# Brute Force Matching
bf = cv2.BFMatcher(cv2.NORM_HAMMING, crossCheck=True)
matches = bf.match(des1, des2)
matches = sorted(matches, key = lambda x:x.distance)
matching_result = cv2.drawMatches(img1, kp1, img2, kp2, matches[:50], None, flags=2)
cv2.imshow("Img1", img1)
cv2.imshow("Img2", img2)
cv2.imshow("Matching result", matching_result)
cv2.imwrite("Matching result.jpg", matching_result)
cv2.waitKey(0)
cv2.destroyAllWindows()
OutPut
Friday, 2 November 2018
Feature Descriptor like ORB, Shift and Surf Implementation using OpenCv Python
import cv2
import numpy as np
img = cv2.imread("2.PNG", cv2.IMREAD_GRAYSCALE)
sift = cv2.xfeatures2d.SIFT_create()
surf = cv2.xfeatures2d.SURF_create()
orb = cv2.ORB_create(nfeatures=1500)
# here None is for non-masking
keypoints1, descriptors1 = orb.detectAndCompute(img, None)
keypoints2, descriptors2 = sift.detectAndCompute(img, None)
keypoints3, descriptors3 = surf.detectAndCompute(img, None)
imgOrb = cv2.drawKeypoints(img, keypoints1, None)
imgSift = cv2.drawKeypoints(img, keypoints2, None)
imgSurf = cv2.drawKeypoints(img, keypoints3, None)
cv2.imshow("Orb", cv2.resize(imgOrb,(700,500)) )
cv2.imshow("Sift", cv2.resize(imgSift,(700,500)) )
cv2.imshow("Surf", cv2.resize(imgSurf,(700,500)) )
cv2.waitKey(0)
cv2.destroyAllWindows()
OutPut
Friday, 19 October 2018
Subscribe to:
Posts (Atom)






