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')
Saturday, 6 July 2019
Video to Image Converter using OpenCV Python
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
Wednesday, 2 May 2018
Simple implementation of SVM in python
import matplotlib.pyplot as plt import numpy as np from sklearn import datasets from sklearn import svm #train set x = [[86,105], [109, 100], [94, 105], [106, 100], [100, 100],
[80, 90], [103, 80], [105, 80], [120, 85], [77, 83], [92, 75],
[98, 76], [106, 82],[106, 77], [105, 77], [119, 80], [115, 70],
[110, 66], [105, 65], [90, 67], [80, 60], [90, 57], [105, 55],
[115, 55], [110, 50], [109, 49], [95, 45], [100, 42], [105, 40],
[110, 42], [115, 42], [115,35], [105, 35], [85, 35], [95, 35], [109, 35],
[115, 35], [120, 30], [105, 29], [109, 25]]
#train set lebels
y = [130, 130, 130, 130, 120, 120, 120, 120, 120, 120, 120, 120, 120, 120, 120,
120, 110, 110, 110, 110, 100, 100, 100, 100, 90, 90, 80, 80, 80, 80, 80, 70,
70, 60, 60, 60, 60, 50, 50, 40] clf = svm.SVC(gamma=0.001, C=100) clf.fit(x,y) from sklearn.externals import joblib joblib.dump(clf, 'C:/Users/kiit1/Desktop/svm.pkl') test_set = [[86,105], [109, 100], [80, 90], [103, 80], [98, 76], [106, 82],
[105, 77], [119, 80], [90, 57], [105, 55], [119, 27]] y_test = [130, 130, 120, 123, 120, 120, 120, 120, 100, 100, 40] # model accuracy for X_testaccuracy = clf.score(test_set, y_test) print(clf.predict(test_set)) print(accuracy) # creating a confusion matrix
from sklearn.metrics import confusion_matrix cm = confusion_matrix(y_test, clf.predict(test_set)) print(cm) clf1 = joblib.load('C:/Users/kiit1/Desktop/svm.pkl') print(clf1.predict([[86,105]]))
Output
Subscribe to:
Posts (Atom)






