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 7b27e2b

Browse files
Google Colab file
1 parent 53a5c68 commit 7b27e2b

File tree

1 file changed

+193
-0
lines changed

1 file changed

+193
-0
lines changed
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 によって変換されたページ (->オリジナル) /