How to extract only CR No only from image - python-3.x

Sample image
I need to extract CR No.from the sample image given above. Using Easyocr, I got the output in complex nested list form. How to update the code to filter out all the detected text/numbers and get only CR No. I am running out of ideas, and help will be appreciated. What I have tried so far-
#Import libraries
import os
import easyocr
import cv2
from matplotlib import pyplot as plt
import numpy as np
IMAGE_PATH = 'H://CDAC//Spyder_projects//CR_No//input_image//input7.jpg'
reader = easyocr.Reader(['en'])
result3 = reader.readtext(IMAGE_PATH)
result3
my_list2 = []
length = len(result3)
for i in range(length):
if (result3[i][1]) == 'CR No':
print(result3[i])
print(result3[i+1])
my_list2.append(result3[i+1]+result3[i])
print(my_list2)
print('The CR No is:', my_list2[0][1])
The expected output should be- 211022203481161

Related

Array Element with Sequence Metpy

Hi there I am getting this error:
ValueError: setting an array element with a sequence.
When I run this code:
import metpy.calc as mpcalc
from metpy.units import units
import cartopy.crs as ccrs
import cartopy.feature as cfeat
import os
from netCDF4 import Dataset as netcdf_dataset
fname = os.path.join(config["repo_data_dir"],
'netcdf', "nam_218_20170625_1200_031.nc"
)
dataset = netcdf_dataset(fname)
lat = dataset.variables['gridlat_0'][:]
lon = dataset.variables['gridlon_0'][:]
sfct=units.degC*(np.mean(dataset.variables['TMP_P0_L103_GLC0'][:,:,:],0)-273.15)
dp=units.degC*(dataset.variables['DPT_P0_L103_GLC0'][:]-273.15)
sfcp=units.hPa*(dataset.variables['PRES_P0_L103_GLC0'][:]/100.0)
lcl_pressure=np.empty(((428),(614)))
lcl_temperature=np.empty(((428),(614)))
lclht=np.empty(((428),(614)))
for i in range(428):
for j in range(614):
lcl_pressure[i,j], lcl_temperature[i,j] = mpcalc.lcl(sfcp[i,j],sfct[i,j],dp[i,j])
lclht[i,j]=mpcalc.pressure_to_height_std(lcl_pressure[i,j]).to('feet')
The line in question is this:
lcl_pressure[i,j], lcl_temperature[i,j] = mpcalc.lcl(sfcp[i,j],sfct[i,j],dp[i,j])
since I am unsure as to how that's causing it as the line in question works for a different code where it says
lcl_pressure[i,j], lcl_temperature[i,j] = mpcalc.lcl(p[0],t[0],dp[0])
as the p,t, and dp in that 2nd version are also arrays

Convert string to matplotlib date for plotting

I've been trying to convert string date into a format that can be plotted on a graph.
The code
import matplotlib.pyplot as plt
import numpy as np
import urllib
import matplotlib.dates as mdates
import datetime
def graph_data():
fig=plt.figure()
ax1=plt.subplot2grid((1,1),(0,0))
stock_price_url = 'https://pythonprogramming.net/yahoo_finance_replacement'
source_code = urllib.request.urlopen(stock_price_url).read().decode()
stock_data = []
split_source=source_code.split('\n')
print(len(split_source))
for line in split_source[1:]:
stock_data.append(line)
date,openn,closep,highp,lowp,openp,volume=np.loadtxt(stock_data,delimiter=',',unpack=True)
x = [datetime.strptime(d, '%Y-%m-%d') for d in date]
ax1.plot_date(x,closep,'-',linewidth=0.1)
plt.xlabel('x')
plt.ylabel('y')
plt.title('Graph')
plt.show()
graph_data()
Any method of conversion just gives the same error
ValueError: could not convert string to float: '2017-07-26'
What method can I use to convert the string into date that can be plotted
Ther's nothing wrong with your code. The problem is with the data.
If you look at the data, you will find that from date to volume features your data is a string like this :
data = '2017-07-26,153.3500,153.9300,153.0600,153.5000,153.5000,12778195.00'.
That is the representation of a string.
So you need to do some preprocessing first. There may be various methods to do so.
I found this method helpful to me:
First, you have to remove the commas in data and replace them with spaces and then use the split function to convert data into a split format.
So, you need to make these changes in your code:
date = []
closep = []
for i in range(len(stock_data)):
temp = stock_data[i].replace(',', ' ').split()
date.append(temp[0])
closep.append(temp[2])
0 and two are the positions of date and closep in your dataset.
Now instead of 'x' and 'closep' as you have used in your plot method, use these 'date ' and 'closep' I just shared you via code.
One more thing is that the graph is having trouble with this big dataset I think.
So use date[0:100], closep[0:100] to try the plot for smaller dataset.
The complete code would look like this:
import matplotlib.pyplot as plt
import numpy as np
import urllib
import matplotlib.dates as mdates
import datetime
def graph_data():
fig = plt.figure()
ax1 = plt.subplot2grid((1, 1), (0, 0))
stock_price_url =
'https://pythonprogramming.net/yahoo_finance_replacement'
source_code = urllib.request.urlopen(stock_price_url).read().decode()
stock_data = []
split_source = source_code.split('\n')
for line in split_source[1:]:
stock_data.append(line)
date = []
closep = []
for i in range(len(stock_data)):
temp = stock_data[i].replace(',', ' ').split()
date.append(temp[0])
closep.append(temp[2])
ax1.plot_date(date[0:100], closep[0:100], '-', linewidth=0.1)
plt.xlabel('x')
plt.ylabel('y')
plt.title('Graph')
plt.show()
graph_data()
Hope this helps.

Sending matplotlib image to pymsteams (cannot create new tag pymsteams)

I am using matplotlib to plot my image.
import pandas as pd
from matplotlib import pyplot as plt
x = ['09:30', '09:33', '09:40', '09:43', '09:50', '09:53', '10:00', '10:03', '10:10', '10:13']
y = ['3010.910000', '3011.650000', '3009.130000', '3011.500000', '3010.460000', '3010.950000', '3012.830000', '3013.120000', '3011.730000', '3010.130000']
matrix = pd.DataFrame({'Time': x, 'Quote': y})
matrix['Quote'] = matrix['Quote'].astype(float)
plt.plot('Time', 'Quote', data=matrix, color='mediumvioletred')
Here is the challenge now:
import pymsteams
web_hook = 'My Microsoft Teams URL https://outlook.office.com/webhook/blahblah'
teams_message = pymsteams.connectorcard(web_hook)
msg_section = pymsteams.cardsection()
msg_section.title('Title')
msg_section.addImage(image) #I want to add that plt image here
teams_message.addSection(msg_section)
teams_message.text("Some Message")
self.teams_message.send()
I have tried this (and I want this approach, using cache):
buf = io.BytesIO()
plt.savefig(buf, format='png')
buf.seek(0)
msg_section.addImage(buf.read())
I did try saving the image to local drive 'c:/temp/'. The code did not give any error msg, but the image on Teams was a blank image, even though the image is correct in c:/temp
In summary. A PNG image has to be converted to base64 string.
Please see the example below.
Note that I'm using Python 3.6.
Additionally image width seems to be limited in a Connector Card.
import numpy as np
import matplotlib.pyplot as plt
import base64
from io import BytesIO
import pymsteams
# generate fig
fig, ax = plt.subplots(1,1,figsize=(20,6))
ax.hist(np.random.normal(0, 1, 1000), bins=51, edgecolor='k', alpha=0.5);
buf = BytesIO()
fig.savefig(buf, format="png")
# get base64 string
data = base64.b64encode(buf.getbuffer()).decode("ascii")
encoded_fig = f"data:image/png;base64,{data}"
# send encoded_fig via webhook
web_hook = 'YOUR_WEBHOOK'
teams_message = pymsteams.connectorcard(web_hook)
msg_section = pymsteams.cardsection()
msg_section.title('Title')
msg_section.addImage(encoded_fig) #I want to add that plt image here
teams_message.addSection(msg_section)
teams_message.text("Some Message")
teams_message.send()
image_file = open('img/icon.png'), "rb").read()
ICON = "data:image/png;base64,{data}".format(data=b64encode(image_file).decode("ascii"))
#And in your Teams alert creation, you call:
section.activityImage(ICON)

cv2 imdecode returns none with base64 string

When i try to convert base64 string into image i am getting none in CV2.
Following is my code
import cv2
import numpy as np
def bsae62toimage(imgvalue):
imge = "R0lGODlhEAAQAMQAAORHHOVSKudfOulrSOp3WOyDZu6QdvCchPGolfO0o/XBs/fNwfjZ0frl3/zy7////wAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACH5BAkAABAALAAAAAAQABAAAAVVICSOZGlCQAosJ6mu7fiyZeKqNKToQGDsM8hBADgUXoGAiqhSvp5QAnQKGIgUhwFUYLCVDFCrKUE1lBavAViFIDlTImbKC5Gm2hB0SlBCBMQiB0UjIQA7"
nparr = np.fromstring(imge,np.uint8)
print(nparr.shape)
img1 = cv2.imdecode(nparr,cv2.IMREAD_UNCHANGED)
print(img1.shape)
It is printing the numpy array but cv2.imdecode is returning None. Can you help me to find the problem in my code?
My Findings: np.fromstring it returns 1D array. I assume it should it should return 3D array. But i may be wrong.
Since it is unclear as from where have you obtained the imge = "R0lGODlhEA..." variable. I would present an ideal flow for converting OpenCV image to base64 string and converting it back to OpenCV img as:
import cv2
import base64
import numpy as np
def to_base64(img):
_, buf = cv2.imencode(".png", img)
return base64.b64encode(buf)
def from_base64(buf):
buf_decode = base64.b64decode(buf)
buf_arr = np.fromstring(buf_decode, dtype=np.uint8)
return cv2.imdecode(buf_arr, cv2.IMREAD_UNCHANGED)
img = cv2.imread("/path/to/img.png")
img_base64 = to_base64(img)
img_decoded = from_base64(img_base64)
print img_decoded.shape
Also the documentation states that:
If the buffer is too short or contains invalid data, the empty
matrix/image is returned.
It seems to me that imge = "R0lGODlhEA..." is invalid.

Import PDF Image From MatPlotLib to ReportLab

I am trying to insert a saved PDF image into a ReportLab flowable.
I have seen several answers to similar questions and many involve using Py2PDF like this:
import PyPDF2
import PIL
input1 = PyPDF2.PdfFileReader(open(path+"image.pdf", "rb"))
page0 = input1.getPage(0)
xObject = page0['/Resources']['/XObject'].getObject()
for obj in xObject:
#Do something here
The trouble I'm having is with a sample image I've saved from MatPlotLib as a PDF. When I try to access that saved image with the code above, it returns nothing under page0['/Resources']['/XObject'].
In fact, here's what I see when I look at page0 and /XObject:
'/XObject': {}
Here's the code I used to generate the PDF:
import matplotlib.pyplot as plt
import numpy as np
# Fixing random state for reproducibility
np.random.seed(19680801)
plt.rcdefaults()
fig, ax = plt.subplots()
# Example data
people = ('Tom', 'Dick', 'Harry', 'Slim', 'Jim')
y_pos = np.arange(len(people))
performance = 3 + 10 * np.random.rand(len(people))
error = np.random.rand(len(people))
ax.barh(y_pos, performance, xerr=error, align='center',
color='green', ecolor='black')
ax.set_yticks(y_pos)
ax.set_yticklabels(people)
ax.invert_yaxis() # labels read top-to-bottom
ax.set_xlabel('Performance')
ax.set_title('How fast do you want to go today?')
plt.savefig(path+'image.pdf',bbox_inches='tight')
Thanks in advance!

Resources