1. Text Detection and Extraction using OpenCV and OCR.
OpenCV (Open source computer vision) is a library mainly aimed at real-time computer vision, in python it helps to process an image and apply various functions e.g. pixel manipulations, object detection, etc. For this project using contours to detect the text in an image and save it to a text file.
Requirements
<span>conda</span> <span>install</span> <span>-</span><span>c</span> <span>conda</span><span>-</span><span>forge</span> <span>opencv</span><span>conda</span> <span>install</span> <span>-</span><span>c</span> <span>conda</span><span>-</span><span>forge</span> <span>pytesseract</span><span>conda</span> <span>install</span> <span>-</span><span>c</span> <span>conda</span><span>-</span><span>forge</span> <span>opencv</span> <span>conda</span> <span>install</span> <span>-</span><span>c</span> <span>conda</span><span>-</span><span>forge</span> <span>pytesseract</span>conda install -c conda-forge opencv conda install -c conda-forge pytesseract
Enter fullscreen mode Exit fullscreen mode
Coding
I will use the following sample image:
<span>import</span> <span>cv2</span><span>import</span> <span>numpy</span> <span>as</span> <span>np</span><span>import</span> <span>pytesseract</span> <span>as</span> <span>pyt</span><span>image</span> <span>=</span> <span>cv2</span><span>.</span><span>imread</span><span>(</span><span>'</span><span>The_Zen_of_Python.jpeg</span><span>'</span><span>)</span><span># image convertion to grey scale </span><span>image_gray</span> <span>=</span> <span>cv2</span><span>.</span><span>cvtColor</span><span>(</span><span>image</span><span>,</span> <span>cv2</span><span>.</span><span>COLOR_BGR2GRAY</span><span>)</span><span>image_gray</span><span>,</span> <span>binary_image</span> <span>=</span> <span>cv2</span><span>.</span><span>threshold</span><span>(</span><span>image_gray</span><span>,</span> <span>128</span><span>,</span><span>255</span><span>,</span> <span>cv2</span><span>.</span><span>THRESH_BINARY</span><span>)</span><span>image_gray</span> <span>=</span><span>cv2</span><span>.</span><span>bitwise_not</span><span>(</span><span>binary_image</span><span>)</span><span>kernel</span> <span>=</span> <span>np</span><span>.</span><span>ones</span><span>((</span><span>2</span><span>,</span><span>1</span><span>),</span> <span>np</span><span>.</span><span>uint8</span><span>)</span><span>image</span> <span>=</span> <span>cv2</span><span>.</span><span>erode</span><span>(</span><span>image_gray</span><span>,</span> <span>kernel</span><span>,</span><span>iterations</span> <span>=</span> <span>1</span><span>)</span><span>image</span> <span>=</span> <span>cv2</span><span>.</span><span>dilate</span><span>(</span><span>image_gray</span><span>,</span> <span>kernel</span><span>,</span><span>iterations</span> <span>=</span> <span>1</span><span>)</span><span>import</span> <span>cv2</span> <span>import</span> <span>numpy</span> <span>as</span> <span>np</span> <span>import</span> <span>pytesseract</span> <span>as</span> <span>pyt</span> <span>image</span> <span>=</span> <span>cv2</span><span>.</span><span>imread</span><span>(</span><span>'</span><span>The_Zen_of_Python.jpeg</span><span>'</span><span>)</span> <span># image convertion to grey scale </span><span>image_gray</span> <span>=</span> <span>cv2</span><span>.</span><span>cvtColor</span><span>(</span><span>image</span><span>,</span> <span>cv2</span><span>.</span><span>COLOR_BGR2GRAY</span><span>)</span> <span>image_gray</span><span>,</span> <span>binary_image</span> <span>=</span> <span>cv2</span><span>.</span><span>threshold</span><span>(</span><span>image_gray</span><span>,</span> <span>128</span><span>,</span><span>255</span><span>,</span> <span>cv2</span><span>.</span><span>THRESH_BINARY</span><span>)</span> <span>image_gray</span> <span>=</span><span>cv2</span><span>.</span><span>bitwise_not</span><span>(</span><span>binary_image</span><span>)</span> <span>kernel</span> <span>=</span> <span>np</span><span>.</span><span>ones</span><span>((</span><span>2</span><span>,</span><span>1</span><span>),</span> <span>np</span><span>.</span><span>uint8</span><span>)</span> <span>image</span> <span>=</span> <span>cv2</span><span>.</span><span>erode</span><span>(</span><span>image_gray</span><span>,</span> <span>kernel</span><span>,</span><span>iterations</span> <span>=</span> <span>1</span><span>)</span> <span>image</span> <span>=</span> <span>cv2</span><span>.</span><span>dilate</span><span>(</span><span>image_gray</span><span>,</span> <span>kernel</span><span>,</span><span>iterations</span> <span>=</span> <span>1</span><span>)</span>import cv2 import numpy as np import pytesseract as pyt image = cv2.imread('The_Zen_of_Python.jpeg') # image convertion to grey scale image_gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) image_gray, binary_image = cv2.threshold(image_gray, 128,255, cv2.THRESH_BINARY) image_gray =cv2.bitwise_not(binary_image) kernel = np.ones((2,1), np.uint8) image = cv2.erode(image_gray, kernel,iterations = 1) image = cv2.dilate(image_gray, kernel,iterations = 1)
Enter fullscreen mode Exit fullscreen mode
The next code will convert the image into a string and print the string as text.
<span># image conversion into string </span><span>image_text</span> <span>=</span> <span>pyt</span><span>.</span><span>image_to_string</span><span>(</span><span>image</span><span>)</span><span>print</span><span>(</span><span>image_text</span><span>)</span><span># image conversion into string </span><span>image_text</span> <span>=</span> <span>pyt</span><span>.</span><span>image_to_string</span><span>(</span><span>image</span><span>)</span> <span>print</span><span>(</span><span>image_text</span><span>)</span># image conversion into string image_text = pyt.image_to_string(image) print(image_text)
Enter fullscreen mode Exit fullscreen mode
Check out the video of the project above –Project video
2. Stock Price Visualisation using Yahoo’s yfinance .
yfinance is an open source library developed by Ran Aroussi as a means to access the financial data available on Yahoo Finance.
Yahoo Finance offers a wide range of market data on stocks, bonds, currencies and cryptocurrencies. In addition it offers market news, reports and analysis and fundamentals data- setting it apart from some of it’s competitors.
Requirements
<span>pip</span> <span>install</span> <span>yfinance</span><span>conda</span> <span>install</span> <span>-</span><span>c</span> <span>conda</span><span>-</span><span>forge</span> <span>pandas</span><span>pip</span> <span>install</span> <span>plotly</span><span>-</span><span>express</span><span>pip</span> <span>install</span> <span>yfinance</span> <span>conda</span> <span>install</span> <span>-</span><span>c</span> <span>conda</span><span>-</span><span>forge</span> <span>pandas</span> <span>pip</span> <span>install</span> <span>plotly</span><span>-</span><span>express</span>pip install yfinance conda install -c conda-forge pandas pip install plotly-express
Enter fullscreen mode Exit fullscreen mode
Coding
<span>import</span> <span>yfinance</span> <span>as</span> <span>yf</span><span>import</span> <span>pandas</span> <span>as</span> <span>pd</span><span>import</span> <span>datetime</span><span>from</span> <span>datetime</span> <span>import</span> <span>date</span><span>,</span> <span>timedelta</span><span>today</span> <span>=</span> <span>date</span><span>.</span><span>today</span><span>()</span><span>date1</span> <span>=</span> <span>today</span><span>.</span><span>strftime</span><span>(</span><span>"</span><span>%Y-%m-%d</span><span>"</span><span>)</span><span>end_date</span> <span>=</span> <span>date1</span><span>date2</span> <span>=</span> <span>date</span><span>.</span><span>today</span><span>()</span> <span>-</span><span>timedelta</span><span>(</span><span>days</span> <span>=</span> <span>365</span><span>)</span><span>date2</span> <span>=</span> <span>date2</span><span>.</span><span>strftime</span><span>(</span><span>"</span><span>%Y-%m-%d</span><span>"</span><span>)</span><span>start_date</span> <span>=</span> <span>date2</span><span>data</span> <span>=</span> <span>yf</span><span>.</span><span>download </span><span>(</span> <span>'</span><span>AAPL</span><span>'</span><span>,</span><span>start</span> <span>=</span> <span>start_date</span><span>,</span><span>end</span> <span>=</span> <span>end_date</span><span>,</span><span>progress</span> <span>=</span> <span>False</span><span>)</span><span>print</span><span>(</span><span>data</span><span>[:</span><span>10</span><span>])</span><span>import</span> <span>yfinance</span> <span>as</span> <span>yf</span> <span>import</span> <span>pandas</span> <span>as</span> <span>pd</span> <span>import</span> <span>datetime</span> <span>from</span> <span>datetime</span> <span>import</span> <span>date</span><span>,</span> <span>timedelta</span> <span>today</span> <span>=</span> <span>date</span><span>.</span><span>today</span><span>()</span> <span>date1</span> <span>=</span> <span>today</span><span>.</span><span>strftime</span><span>(</span><span>"</span><span>%Y-%m-%d</span><span>"</span><span>)</span> <span>end_date</span> <span>=</span> <span>date1</span> <span>date2</span> <span>=</span> <span>date</span><span>.</span><span>today</span><span>()</span> <span>-</span><span>timedelta</span><span>(</span><span>days</span> <span>=</span> <span>365</span><span>)</span> <span>date2</span> <span>=</span> <span>date2</span><span>.</span><span>strftime</span><span>(</span><span>"</span><span>%Y-%m-%d</span><span>"</span><span>)</span> <span>start_date</span> <span>=</span> <span>date2</span> <span>data</span> <span>=</span> <span>yf</span><span>.</span><span>download </span><span>(</span> <span>'</span><span>AAPL</span><span>'</span><span>,</span> <span>start</span> <span>=</span> <span>start_date</span><span>,</span> <span>end</span> <span>=</span> <span>end_date</span><span>,</span> <span>progress</span> <span>=</span> <span>False</span><span>)</span> <span>print</span><span>(</span><span>data</span><span>[:</span><span>10</span><span>])</span>import yfinance as yf import pandas as pd import datetime from datetime import date, timedelta today = date.today() date1 = today.strftime("%Y-%m-%d") end_date = date1 date2 = date.today() -timedelta(days = 365) date2 = date2.strftime("%Y-%m-%d") start_date = date2 data = yf.download ( 'AAPL', start = start_date, end = end_date, progress = False) print(data[:10])
Enter fullscreen mode Exit fullscreen mode
Visualise your Stock price data using Plotly Express. This visualisation will prompt a new window to open in your browser with up todate data.
<span>import</span> <span>plotly.express</span> <span>as</span> <span>px</span><span>fig</span> <span>=</span> <span>px</span><span>.</span><span>line</span><span>(</span><span>data</span><span>,</span> <span>x</span> <span>=</span> <span>data</span><span>.</span><span>index</span><span>,</span><span>y</span> <span>=</span> <span>"</span><span>Close</span><span>"</span><span>,</span><span>title</span> <span>=</span> <span>"</span><span>Stock Price data</span><span>"</span><span>)</span><span>fig</span><span>.</span><span>show</span><span>()</span><span>import</span> <span>plotly.express</span> <span>as</span> <span>px</span> <span>fig</span> <span>=</span> <span>px</span><span>.</span><span>line</span><span>(</span><span>data</span><span>,</span> <span>x</span> <span>=</span> <span>data</span><span>.</span><span>index</span><span>,</span> <span>y</span> <span>=</span> <span>"</span><span>Close</span><span>"</span><span>,</span> <span>title</span> <span>=</span> <span>"</span><span>Stock Price data</span><span>"</span><span>)</span> <span>fig</span><span>.</span><span>show</span><span>()</span>import plotly.express as px fig = px.line(data, x = data.index, y = "Close", title = "Stock Price data") fig.show()
Enter fullscreen mode Exit fullscreen mode
暂无评论内容