Using ImageMagick/ZBar to read QR codes - imagemagick

I've got scanned image files that I perform some preprocessing on and get them looking something like this:
My phone's ZBar app can read this QR code fine, but zbarimg seems to be unable to figure it out. I've tried all sorts of things in ImageMagick to make it smoother (-smooth, -morphology) but even with slightly better-looking results, zbarimg still comes up blank.
Why would my phone's ZBar be so much better than my computer's (zbar-0.10)? Is there anything I can do to get zbarimg to read this successfully?

You can try morphological closing.
Python code:
# -*- coding: utf-8 -*-
import qrtools
import cv2
import numpy as np
imgPath = "Fdnm1.png"
img = cv2.imread(imgPath, 0)
kernel = np.ones((5, 5), np.uint8)
processed=cv2.morphologyEx(img, cv2.MORPH_CLOSE, kernel)
cv2.imwrite('test.png', processed)
d = qrtools.QR(filename='test.png')
d.decode()
print d.data
Result:
1MB24

Related

The use of librosa.effects.trim to remove the silent part in audio

I am doing a speech emotion recognition ML.
I currently use pyAudioAnalysis to do a multi-directory feature extraction. However, the dataset involved in audios containing a lot of approximately silent sections. My objective is to remove the approximately silent parts from all the audios then extract meaningful features.
My current approach is to use librosa to trim the silent parts.
from librosa.effects import trim
import librosa
from pyAudioAnalysis import audioBasicIO
import matplotlib.pyplot as plt
signal, Fs = librosa.load(file_directory)
trimed_signal = trim(signal,top_db=60)
fig, ax = plt.subplots(nrows=3, sharex=True, sharey=True)
librosa.display.waveplot(trimed_signal, sr=Fs, ax=ax[0])
ax[0].set(title='Monophonic')
ax[0].label_outer()
I tried to plot the wave after trimming using librosa.display.waveplot but an AttributeError occurred showing AttributeError: module 'librosa' has no attribute 'display'
My questions are
How to plot the trimmed wave?
Is it possible to generate a trimmed .wav file? This is because pyAudioAnalysis's input for feature extraction is .wav file path but the output of librosa is array.
You need to import librosa.display separately. See this issue for the reason.
You can use librosa.output.write_wav (check the docs) to store the trimmed array as a wave file. E.g. librosa.output.write_wav(path, trimed_signal, Fs).

Displaying frames in a form of video on google colab

I am a trying video processing on google colab. My code read the video and break it into frames and after the processing on the frame I want to display the video as frames are processed. Like what cv2.imshow does (on local computer). But cv2.imshow gives error in colab so as it suggested I used cv2_imshow using from google.colab.patches import cv2_imshow . It is displaying the frames but in an column(like separate images) but replacing the previous displayed. Here is my colab link: https://colab.research.google.com/drive/1RUOGahcGngTWG9nBoisrsPzCLQ1Jq88v?usp=sharing
You can see the output at the end of the page where multiple images are.
Any help is really appreciated :)
try:
from google.colab.patches import cv2_imshow
from IPython.display import clear_output
from time import sleep
clear_output()
cv2_imshow(img)
sleep(0.1)
it's far from perfect (since there are some frame drops for some reason), but that's the closest thing I could find.

can't show an image using PIL on google colab

I am trying to use PIL to show an image. I know that I can use other modules to do that. I am working on google colab. But I can't figure out why PIL is not showing output image.
% matplotlib inline
import numpy as np
import PIL
im=Image.open('/content/drive/My Drive/images-process.jpeg')
print(im.width, im.height, im.mode, im.format, type(im))
im.show()
output: 739 415 RGB JPEG < class 'PIL.JpegImagePlugin.JpegImageFile'>
Instead of
im.show()
Try just
im
Colab should try to display it on its own. See example notebook
Use
display(im)
instead of im.show() or im.
When using these options after multiple lines or in a loop, im won't work.
After you open an image(which you have done using Image.open()), try converting using im.convert() to which ever mode image is in then do display(im)
It will work

Using numba functions in map_blocks

I have successfully used map_blocks a few times on dask arrays. I'm now trying to deploy a numba function to act on each block, and to act and change one of the inputs.
The numba function takes in 2 numpy arrays, and updates the second one. this is then returned in the return statement to make it available to map_blocks as a result.
The function works fine on a numpy array, but python just crashes when calling it from map_blocks. numba functions that do not act on an input array behave normally (although it is difficult to get them to do anything useful in this case).
Is this a known limitation? A bug? Am I using it wrong?!
Update
I've finally boiled it down to a reproducible example with a trivial numba function, and I get a clearer idea of the problem. However I'm still unclear on how to resolve the issue. Here's the code:
import numpy as np
from numba import jit, float64, int64
from dask.distributed import Client, LocalCluster
import dask.array as da
cluster=LocalCluster()
c=Client(cluster)
size=int(1e5)
a=np.arange(size,dtype='float64')
b=np.zeros((size,),dtype='float64')
dista=da.from_array(a,chunks=size//4)
distb=da.from_array(b,chunks=size//4)
#jit(float64[:](float64[:],float64[:]))
def crasher(x,y):
for i in range(x.shape[0]):
y[i]=x[i]*2
return y
distc=da.map_blocks(crasher,dista,distb,dtype='float64')
c=distc.compute() #it all crashes at this point
And I now get a more comprehensible error rather than just a straight up crash:
TypeError: No matching definition for argument type(s) readonly array(float64, 1d, C), readonly array(float64, 1d, C)
So if numba is receiving numpy arrays with write=False set, how do you get numba to do any useful work? You can't put an array creation line in the numba function, and you can't feed it writeable arrays.
Any views on how to achieve this?
Here is a version of your code with array creation, which runs fine with numba nopython mode
import numpy as np
from numba import jit, float64, int64
from dask.distributed import Client, LocalCluster
import dask.array as da
cluster=LocalCluster()
c=Client(cluster)
size=int(1e5)
a=np.arange(size,dtype='float64')
dista=da.from_array(a,chunks=size//4)
#jit(nopython=True)
def crasher(x):
y = np.empty_like(x)
for i in range(x.shape[0]):
y[i]=x[i]*2
return y
distc=da.map_blocks(crasher,dista,dtype='float64')
c=distc.compute()
Note the y= line. Note the list of numpy functions supported, according to the documentation.

defaulting output behavior to print in ipython console

What setting in spyder should I change such that when I submit this code, I get identical looking output for both print call ?
import pandas as pd
df = pd.Dataframe({'a':[1,2,3], 'b':[2,3,4]})
df.shape
print df.shape
I suspect that df.shape isn't exactly the same as print df.shape but on any other installation of Spyder that I have used, both call behave the same. I am using python 2.7.12, conda 4.1.11 and pandas 0.18.1
Currently print df.shape correctly prints the shape of df in the console. df.shape is displaying the shape of df in a bigger and dark font in the console.
A picture is worth a 1000 words, what I am getting currently looks like this:

Resources