Skip to content

Added code reflecting to ocr functioning #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
254 changes: 254 additions & 0 deletions OCR using google vision api/google-vision-api-notebook.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,254 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {},
"colab_type": "code",
"id": "I86tgLoXN-fN"
},
"outputs": [],
"source": [
"import cv2\n",
"import imutils\n",
"import json\n",
"import matplotlib.pyplot as plt\n",
"import numpy as np\n",
"import os\n",
"import pandas as pd\n",
"import requests\n",
"import time\n",
"from base64 import b64encode\n",
"from IPython.display import Image\n",
"from pylab import rcParams"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {},
"colab_type": "code",
"id": "87ckNcIOiIjf"
},
"outputs": [],
"source": [
"rcParams['figure.figsize'] = 10, 20"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {},
"colab_type": "code",
"id": "j1x8BO5AORgS"
},
"outputs": [],
"source": [
"def makeImageData(imgpath):\n",
" img_req = None\n",
" with open(imgpath, 'rb') as f:\n",
" ctxt = b64encode(f.read()).decode()\n",
" img_req = {\n",
" 'image': {\n",
" 'content': ctxt\n",
" },\n",
" 'features': [{\n",
" 'type': 'DOCUMENT_TEXT_DETECTION',\n",
" 'maxResults': 1\n",
" }]\n",
" }\n",
" return json.dumps({\"requests\": img_req}).encode()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {},
"colab_type": "code",
"id": "5PH_sr9TN7eS"
},
"outputs": [],
"source": [
"def requestOCR(url, api_key, imgpath):\n",
" imgdata = makeImageData(imgpath)\n",
" response = requests.post(ENDPOINT_URL,\n",
" data = imgdata,\n",
" params = {'key': api_key},\n",
" headers = {'Content-Type': 'application/json'})\n",
" return response"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {},
"colab_type": "code",
"id": "ia3SZkwhQi8t"
},
"outputs": [],
"source": [
"with open('vision_api.json') as f:\n",
" data = json.load(f)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {},
"colab_type": "code",
"id": "ziTG4b1-N0oU"
},
"outputs": [],
"source": [
"ENDPOINT_URL = 'https://vision.googleapis.com/v1/images:annotate'\n",
"api_key = data[\"api_key\"]\n",
"img_loc = \"Image.jpg\""
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {},
"colab_type": "code",
"id": "-RlRp1bllxbh"
},
"outputs": [],
"source": [
"Image(img_loc)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {},
"colab_type": "code",
"id": "EU1SEPZWlhGt"
},
"outputs": [],
"source": [
"result = requestOCR(ENDPOINT_URL, api_key, img_loc)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {},
"colab_type": "code",
"id": "mfZ_KT68OgV7"
},
"outputs": [],
"source": [
"if result.status_code != 200 or result.json().get('error'):\n",
" print (\"Error\")\n",
"else:\n",
" result = result.json()['responses'][0]['textAnnotations']"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {},
"colab_type": "code",
"id": "KlGzGqOzROk3"
},
"outputs": [],
"source": [
"result"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {},
"colab_type": "code",
"id": "o5AYbd9YfwL-"
},
"outputs": [],
"source": [
"for index in range(len(result)):\n",
" print(result[index][\"description\"])"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {},
"colab_type": "code",
"id": "iIs5LgaAfwQ6"
},
"outputs": [],
"source": [
"def gen_cord(result):\n",
" cord_df = pd.DataFrame(result['boundingPoly']['vertices'])\n",
" x_min, y_min = np.min(cord_df[\"x\"]), np.min(cord_df[\"y\"])\n",
" x_max, y_max = np.max(cord_df[\"x\"]), np.max(cord_df[\"y\"])\n",
" return result[\"description\"], x_max, x_min, y_max, y_min"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {},
"colab_type": "code",
"id": "Q4LYtrQffm77"
},
"outputs": [],
"source": [
"text, x_max, x_min, y_max, y_min = gen_cord(result[-1])\n",
"image = cv2.imread(img_loc)\n",
"cv2.rectangle(image,(x_min,y_min),(x_max,y_max),(0,255, 0),2)\n",
"plt.imshow(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))\n",
"print (\"Text Detected = {}\".format(text))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {},
"colab_type": "code",
"id": "f3NoY3tCjDdZ"
},
"outputs": [],
"source": []
}
],
"metadata": {
"colab": {
"name": "Untitled0.ipynb",
"provenance": []
},
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.5"
}
},
"nbformat": 4,
"nbformat_minor": 1
}
775 changes: 775 additions & 0 deletions syntax_exercises_found_ling.ipynb

Large diffs are not rendered by default.