-1

I am doing a project in which I have image of electricity meter reading. I need to extract the digits in the image.

I converted the image to a numpy array using the PIL Image function.

This is the code I typed

import numpy as np
from PIL import Image
img_data = Image.open('meter1.jpg' )
img_arr = np.array(img_data) 
print(img_arr)

I got this numpy array as the output

[[[  2  96  10]
  [  2  96  10]
  [  2  96  10]
  ...
  [ 18 144  47]
  [ 13 141  48]
  [ 10 139  46]]

 [[ 11 105  19]
  [ 10 106  19]
  [ 10 104  18]
  ...
  [ 28 156  59]
  [ 26 156  60]
  [ 24 155  59]]

 [[ 19 115  26]
  [ 16 115  25]
  [ 17 113  24]
  ...
  [ 30 162  60]
  [ 28 164  62]
  [ 26 165  64]]

  ...

 [[  0 126  18]
  [  0 126  18]
  [  0 126  18]
  ...
  [  4 211  77]
  [  4 211  79]
  [  6 213  83]]

 [[  0 126  18]
  [  0 126  18]
  ...
  [  4 212  76]
  [  4 211  79]
  [  6 213  83]]

 [[  1 124  17]
  [  1 124  17]
  [  1 124  17]
  ...
  [  5 211  76]
  [  5 210  79]
  [  7 212  81]]]

How do I use this numpy array to extract the numerical values or the digits or the numbers from this image?

enter image description here

It is a seven segment display. Was is useful to convert the image to numpy array? Is there any other approach to do this. I have not done much of hand-on python so please help

4
  • Could you please add a plain python tag? Commented Oct 11, 2020 at 5:52
  • Does this answer your question? Extracting digits from image with python and OpenCV Commented Oct 11, 2020 at 5:54
  • The code from "Extracting digits from image with python and OpenCV" does not seem to work on my image Commented Oct 11, 2020 at 6:23
  • AttributeError: 'NoneType' object has no attribute 'reshape' . I am getting this error please help Commented Oct 11, 2020 at 8:55

1 Answer 1

0

it's better first to try doing some coding, because this way your coding skills improve. by the way, I wrote a script that save your digits into separate image files. hope it helps you in your project and improving your skills.

import numpy as np
from PIL import Image
import os
directory,filename = os.path.split(__file__)
#x = np.array([ [ [255,0,0], [0,255,0], [0,0,255] ],[ [0,0,0],[128,128,128],[2,5,200] ] ],dtype=np.uint8)
main_img = Image.open('img.jpg')
x = np.array(main_img)
print(x)
#print(x[1][1] )# x[row number][column number]
#print(x.shape[1] )# x.shape = # of rows,# of cols
data_cols = []
bg_color = x[0][0]
start = False
start_id = -1
for j in range(0,x.shape[1]):
    for i in range(0,x.shape[0]):
        if (x[i][j][0] < 5) and (x[i][j][2] < 10):
            if not start:
                start_id = j
                start = True
            break
        if i == x.shape[0]-1:
            start = False
            end_id = j
            if start_id>=0:
                data_cols.append([start_id,end_id])
                start_id=-1
print("Number of digits>",len(data_cols))
images = []
for i in range(0,len(data_cols)):
    images.append(x[:,data_cols[i][0]:data_cols[i][1]])
i = 0
for im_array in images:
    im = Image.fromarray(im_array,'RGB')
    im.save(directory + "\\" + str(i) + ".png")
    i += 1
    
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.