Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 9497884

Browse files
Merge pull request avinashkranjan#527 from ShubhamGupta577/Extracting-text-from-a-real-time-captured-image
Extracting text from a real time captured image (avinashkranjan#497)
2 parents 8f81c59 + 9f660aa commit 9497884

File tree

3 files changed

+257
-0
lines changed

3 files changed

+257
-0
lines changed

‎Realtime Text Extraction/Readme.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
## Text Extraction
2+
In this script we are going to read a real-time captured image using a web cam or any other camera and extract the text from that image by removing noise from it.
3+
This script uses `OpenCV` for capturing the real-time image and `pytesseract` for text extraction from the captured image.
4+
5+
## Setup instructions
6+
7+
#### You can use this in two ways
8+
* If your machine has any python intrepretor than you can go for [**Real-time Text Extraction**](https://github.com/ShubhamGupta577/Amazing-Python-Scripts/blob/Extracting-text-from-a-real-time-captured-image/Realtime%20Text%20Extraction/Realtime%20Text%20Extraction.py)
9+
* For these you need to install **OpenCV**, **pytesseracrt** and **PIL** in your machine.
10+
* Another way is to use the [**Google Colab file**](https://github.com/ShubhamGupta577/Amazing-Python-Scripts/blob/Extracting-text-from-a-real-time-captured-image/Realtime%20Text%20Extraction/Realtime_Text_Extracion.ipynb) which will be excecute on a cloud. No need of any external downloading
11+
* Just download the files and follow the instructions given in the files
12+
13+
14+
## Author
15+
16+
[Shubham Gupta](https://github.com/ShubhamGupta577)
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#importing required libraries
2+
3+
import cv2
4+
import pytesseract
5+
from PIL import Image
6+
7+
#Put the path where you stored the tesseract.exe file in your machine
8+
pytesseract_file=input("Enter the path where you stored the tesseract.exe file\n")
9+
pytesseract.pytesseract.tesseract_cmd = pytesseract_file
10+
11+
12+
cam = cv2.VideoCapture(0) #Starting your webcam
13+
14+
while True:
15+
ret, img = cam.read() #Capturing the image
16+
gray= cv2.cvtColor(img,cv2.COLOR_BGR2GRAY) #Can convert colour image to grayscale
17+
18+
cv2.imshow("Original",img)
19+
#Removing noise from the image
20+
ret, thresh = cv2.threshold(gray,1010, 200, cv2.THRESH_OTSU, cv2.THRESH_BINARY)
21+
cv2.imshow("After removing noise", thresh)
22+
23+
if not ret:
24+
break
25+
26+
k=cv2.waitKey(1) #Taking input from you
27+
28+
if k%256==27: #Press Esc for exit
29+
#For Esc key
30+
print("Close")
31+
break
32+
33+
elif k%256==32: #Press Space for capture the image
34+
#For Space key
35+
print("Image saved")
36+
#Put the path where you want to store the captured image in your machine
37+
path=input("Enter the path where you want to store the image\n ")
38+
path=path+'\img.jpg'
39+
cv2.imwrite(path, thresh)
40+
break
41+
42+
src= Image.open(path)
43+
44+
text= pytesseract.image_to_string(src) #Extracting the text from image
45+
print(text)
46+
47+
cam.release
48+
cv2.destroyAllWindows
Lines changed: 193 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,193 @@
1+
{
2+
"nbformat": 4,
3+
"nbformat_minor": 0,
4+
"metadata": {
5+
"colab": {
6+
"name": "Realtime Text Extracion.ipynb",
7+
"provenance": [],
8+
"collapsed_sections": []
9+
},
10+
"kernelspec": {
11+
"display_name": "Python 3",
12+
"name": "python3"
13+
}
14+
},
15+
"cells": [
16+
{
17+
"cell_type": "markdown",
18+
"metadata": {
19+
"id": "lv8kfltaKySW"
20+
},
21+
"source": [
22+
"##**TEXT EXTRACTION FROM A REAL TIME CAPTURED IMAGE**"
23+
]
24+
},
25+
{
26+
"cell_type": "markdown",
27+
"metadata": {
28+
"id": "wWB6a7k7IQ4W"
29+
},
30+
"source": [
31+
"###**Code for starting the webcam in the google colab**"
32+
]
33+
},
34+
{
35+
"cell_type": "code",
36+
"metadata": {
37+
"id": "SucxddsPhOmj"
38+
},
39+
"source": [
40+
"from IPython.display import display, Javascript\n",
41+
"from google.colab.output import eval_js\n",
42+
"from base64 import b64decode\n",
43+
"\n",
44+
"def take_photo(filename='photo.jpg', quality=0.8):\n",
45+
" js = Javascript('''\n",
46+
" async function takePhoto(quality) {\n",
47+
" const div = document.createElement('div');\n",
48+
" const capture = document.createElement('button');\n",
49+
" capture.textContent = 'Capture';\n",
50+
" div.appendChild(capture);\n",
51+
"\n",
52+
" const video = document.createElement('video');\n",
53+
" video.style.display = 'block';\n",
54+
" const stream = await navigator.mediaDevices.getUserMedia({video: true});\n",
55+
"\n",
56+
" document.body.appendChild(div);\n",
57+
" div.appendChild(video);\n",
58+
" video.srcObject = stream;\n",
59+
" await video.play();\n",
60+
"\n",
61+
" // Resize the output to fit the video element.\n",
62+
" google.colab.output.setIframeHeight(document.documentElement.scrollHeight,true);\n",
63+
"\n",
64+
" // Wait for Capture to be clicked.\n",
65+
" await new Promise((resolve) => capture.onclick = resolve);\n",
66+
"\n",
67+
" const canvas = document.createElement('canvas');\n",
68+
" canvas.width = video.videoWidth;\n",
69+
" canvas.height = video.videoHeight;\n",
70+
" canvas.getContext('2d').drawImage(video, 0, 0);\n",
71+
" stream.getVideoTracks()[0].stop();\n",
72+
" div.remove();\n",
73+
" return canvas.toDataURL('image/jpeg', quality);\n",
74+
" }\n",
75+
" ''')\n",
76+
" display(js)\n",
77+
" data = eval_js('takePhoto({})'.format(quality))\n",
78+
" binary = b64decode(data.split(',')[1])\n",
79+
" with open(filename, 'wb') as f:\n",
80+
" f.write(binary)\n",
81+
" return filename"
82+
],
83+
"execution_count": 4,
84+
"outputs": []
85+
},
86+
{
87+
"cell_type": "markdown",
88+
"metadata": {
89+
"id": "KxGGlrg-K_Wz"
90+
},
91+
"source": [
92+
"####Click on the **Capture** button for capturing the image"
93+
]
94+
},
95+
{
96+
"cell_type": "code",
97+
"metadata": {
98+
"id": "buJCl90WhNfq"
99+
},
100+
"source": [
101+
"from IPython.display import Image\n",
102+
"try:\n",
103+
" filename = take_photo()\n",
104+
" print('Saved to {}'.format(filename))\n",
105+
" \n",
106+
" # Show the image which was just taken.\n",
107+
" display(Image(filename))\n",
108+
"except Exception as err:\n",
109+
" # Errors will be thrown if the user does not have a webcam or if they do not\n",
110+
" # grant the page permission to access it.\n",
111+
" print(str(err))"
112+
],
113+
"execution_count": null,
114+
"outputs": []
115+
},
116+
{
117+
"cell_type": "markdown",
118+
"metadata": {
119+
"id": "V-95YMbaIjCL"
120+
},
121+
"source": [
122+
"###**Installing and importing the required libraries**"
123+
]
124+
},
125+
{
126+
"cell_type": "code",
127+
"metadata": {
128+
"id": "TPetMlwdvspP"
129+
},
130+
"source": [
131+
"!sudo apt install tesseract-ocr\r\n",
132+
"!pip install pytesseract\r\n",
133+
"import pytesseract as ts\r\n",
134+
"import cv2\r\n",
135+
"from google.colab.patches import cv2_imshow"
136+
],
137+
"execution_count": null,
138+
"outputs": []
139+
},
140+
{
141+
"cell_type": "code",
142+
"metadata": {
143+
"id": "c5UJmjW8wbZ9"
144+
},
145+
"source": [
146+
"'''\r\n",
147+
"If there is error occured as path is not defined or you get the wrong image\r\n",
148+
"Follow the given steps\r\n",
149+
"Put the path of the picture captured by you\r\n",
150+
"You can find it in the files part under the navigation in the left side\r\n",
151+
"Right click on the photo, click on \"copy path\" and paste it here \r\n",
152+
"'''\r\n",
153+
"\r\n",
154+
"img=cv2.imread('/content/photo.jpg')\r\n",
155+
"gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)\r\n",
156+
"cv2_imshow(img)\r\n",
157+
"#Removing noise from the image\r\n",
158+
"ret, thresh = cv2.threshold(gray,1010, 200, cv2.THRESH_OTSU, cv2.THRESH_BINARY)\r\n",
159+
"text = ts.image_to_string(thresh) \r\n",
160+
"\r\n",
161+
"cv2_imshow(thresh) #printing realtime text\r\n",
162+
"print(\"\\n\\n\")\r\n",
163+
"print(text)\r\n",
164+
"\r\n",
165+
"#If the desried output is not obtained just change the 'thresh to 'gray' in line 13 and 15"
166+
],
167+
"execution_count": null,
168+
"outputs": []
169+
},
170+
{
171+
"cell_type": "code",
172+
"metadata": {
173+
"id": "fdVdr4Ka05FG"
174+
},
175+
"source": [
176+
""
177+
],
178+
"execution_count": null,
179+
"outputs": []
180+
},
181+
{
182+
"cell_type": "code",
183+
"metadata": {
184+
"id": "jUyVjAV9w3iG"
185+
},
186+
"source": [
187+
""
188+
],
189+
"execution_count": null,
190+
"outputs": []
191+
}
192+
]
193+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /