Image resize becoming half of its original size

493 Views Asked by At

How to fix image not getting damaged while trying to resize (mogrify) the image itself?

From this: Raw Image

After mogrify-ing through my python script: Resize Image

Basically, what I'm trying to accomplish here is to; first, capture an image from my Pi Camera, then the raw image will be converted into a text format (OCR). The image has to be at some certain size in order for the library to recognize the text written in the image itself, resizing it prior to conversion yield no result at all. The result will then be fetch to my external database. Lastly, the raw image will then be resize through mogrify-ing. I had to resize it before sending the image to database due to some error.

Here's the script for OCR:

def ocrFunction():
 img = cv2.imread('try.png',cv2.IMREAD_COLOR)

 img = cv2.resize(img, (620,480) )

 gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) #convert to grey scale
 gray = cv2.bilateralFilter(gray, 11, 17, 17) #Blur to reduce noise
 edged = cv2.Canny(gray, 30, 200) #Perform Edge detection

 # find contours in the edged image, keep only the largest
 # ones, and initialize our screen contour

 cnts = cv2.findContours(edged.copy(), cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
 cnts = imutils.grab_contours(cnts)
 cnts = sorted(cnts, key = cv2.contourArea, reverse = True)[:10]
 screenCnt = None

# loop over our contours
 for c in cnts:
  # approximate the contour
  peri = cv2.arcLength(c, True)
  approx = cv2.approxPolyDP(c, 0.018 * peri, True)

  # if our approximated contour has four points, then
  # we can assume that we have found our screen
  if len(approx) == 4:
   screenCnt = approx
   break

 if screenCnt is None:
  detected = 0
  print ("No contour detected")
 else:
  detected = 1

 if detected == 1:
  cv2.drawContours(img, [screenCnt], -1, (0, 255, 0), 3)

 # Masking the part other than the number plate
 mask = np.zeros(gray.shape,np.uint8)
 new_image = cv2.drawContours(mask,[screenCnt],0,255,-1,)
 new_image = cv2.bitwise_and(img,img,mask=mask)

 # Now crop
 (x, y) = np.where(mask == 255)
 (topx, topy) = (np.min(x), np.min(y))
 (bottomx, bottomy) = (np.max(x), np.max(y))
 Cropped = gray[topx:bottomx+1, topy:bottomy+1]

 #Read the number plate
 text = pytesseract.image_to_string(Cropped, config='--psm 11')
 print("Detected Number is:",text)
 file = text

 writeConvert(file,'TextC.txt')
 uploadData("/home/pi/try.png","/home/pi/TextC.txt")

I'm trying to get the written text from "try.png" as my sample from the image above.

And here's the script for resizing the image together with uploading data to my external database:

def uploadData(vehicle_plate_image, vehicle_plate_text):
 import subprocess
 subprocess.call("mogrify -resize 600x450 /home/pi/try.png", shell=True)

 conn = None
 try:
  conn = mysql.connector.connect(
   host='192.168.1.5',
   user='root',
   password='password',
   charset='utf8',
   port=3306
  )
  if conn.is_connected():
   print('Connected to MySql')

   cur = conn.cursor()
   cur = conn.cursor(buffered=True)
   query = '''INSERT INTO db_gopark.tbl_vehicle (`vehicle_id`, `vehicle_plate_image`,`vehicle_plate_text`) VALUES (NULL,%s,%s);'''
   #image Conversion
   vehicle_plate_image = convertToBinaryData('/home/pi/try.png')
   vehicle_plate_text = convertToBinaryData('/home/pi/TextC.txt')
   # Convert data into tuple format
   image_blob_tuple = (vehicle_plate_image,vehicle_plate_text)
   #print( image_blob_tuple)
   result = cur.execute(query, image_blob_tuple)
   print('Success Inserting',result)
   conn.commit()
   cur.close()

 except Error as e:
  print(e)
  #print("Failed inserting BLOB data into MySQL table {}".format(Error))

 finally:
  if conn is not None and conn.is_connected():
   print('Not Connected')
   conn.close()
 

I'm almost done with this though, it is just that the image keeps on getting damaged after trying to resize it. I also tried using different sample images, the result are always the same.

0

There are 0 best solutions below