Find and Crop using OpenCV

To get the value on a display we first need to crop the display in the image.
This is an example source image: The only Python package used is opencv.

Import OpenCV:

import cv2

Load image and get middle of image:

image = image[300:1300, 500:1400]
height, width, channels = image.shape Rotate image:

center = (width / 2, height / 2)
angle = 87
M = cv2.getRotationMatrix2D(center, angle, 1)
image = cv2.warpAffine(image, M, (height, width)) Convert to grayscale:

gray = cv2.cvtColor(image.copy(), cv2.COLOR_BGR2GRAY) Blur and threshold:

gray = cv2.blur(gray, (11, 11))
thresh = cv2.threshold(gray, 60, 255, cv2.THRESH_BINARY) Find contour of box in the middle of the image and plot in green on image. The box has a minimum of 100 pixel width and a minimum of 50 pixel height.

contours,hierarchy = cv2.findContours(thresh, 1, 2)
for cnt in contours:
x,y,w,h = cv2.boundingRect(cnt)
if w>100 and h>50:
break Now crop image based on the points of the green box:

cropped = image[y:y+h, x:x+w] Increase alpha contrast of the image to see the digits and save result to disk:

contrast = cv2.convertScaleAbs(cropped, alpha=3, beta=0)
cv2.imwrite('cropped_image.png', cropped) The next step will be to recognize the numbers in the display.

Get GPS data from images

I wanted to extract the gps coordinates of jpg images. I found this solution and modified it for my needs: https://gist.github.com/snakeye/fdc372dbf11370fe29eb

import sys

def convert_to_degress(value):
d = float(value.values.num) / float(value.values.den)
m = float(value.values.num) / float(value.values.den)
s = float(value.values.num) / float(value.values.den)

return d + (m / 60.0) + (s / 3600.0)

def get_location(exif_data):
latitude = exif_data.get('GPS GPSLatitude')
latitude_ref = exif_data.get('GPS GPSLatitudeRef')
longitude = exif_data.get('GPS GPSLongitude')
longitude_ref = exif_data.get('GPS GPSLongitudeRef')

if not (latitude and latitude_ref and longitude and longitude_ref):
return None, None

lat = convert_to_degress(latitude)
if latitude_ref.values != 'N':
lat = 0 - lat

lon = convert_to_degress(longitude)
if longitude_ref.values != 'E':
lon = 0 - lon

return lat, lon

with open(sys.argv, 'rb') as f:
lat, long = get_location(exif_tags)
print(lat, long)

This code reads a JPG image and prints latitude and longitude in decimals of that image.

AllenNLP: Run on Production with Django

This post describes howto run a custom AllenNLP model within Django and Django Rest Framework.

The View code:

import json

from rest_framework import status, viewsets
from rest_framework.parsers import JSONParser
from rest_framework.response import Response

from .utils import predict

class PredictViewSet(viewsets.ViewSet):
parser_classes = (JSONParser,)

def create(self, request):
"""
example call:
curl -X POST "http://localhost:8000/" -H "content-type: application/json" --data '{"source": "1/5/2003"}'
"""
if isinstance(request.data, str):
else:
data = request.data

if not "source" in data:
return Response(
)
return Response(predict(data.get("source")))

And the utils.py with the predict method.

from allennlp.common.util import import_submodules
from allennlp.predictors import Predictor

def predict(source):
# register custom model code
# the library code is in the same Django app (main) in the folder "library"
import_submodules("main.library")

# load model without using cuda