Last active
January 14, 2023 13:47
-
-
Save freedomofkeima/519dcd0e9fbbf2518379236a1f7a2373 to your computer and use it in GitHub Desktop.
Extract GPSMAP data
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import cv2 | |
import numpy as np | |
import pafy | |
import pytesseract | |
TESSERACT_PARAMETERS = '--oem 3 --psm 6 -c tessedit_char_whitelist="Ckmtv0123456789.:"' | |
def cleanup_dist(dist: str) -> str: | |
separator_position = dist.find(".") | |
if separator_position != -1: | |
return dist[:separator_position+3] | |
return "?" | |
def cleanup_temperature(temperature: str) -> str: | |
temperature = temperature.replace(",", ".") | |
separator_position = temperature.find(".") | |
if separator_position != -1: | |
return f"{temperature[:separator_position+3]} °C" | |
return "?" | |
def cleanup_speed(speed: str) -> str: | |
speed = speed.replace("_", "").replace(" ", "").replace(",", ".") | |
separator_position = speed.find(".") | |
if separator_position != -1: | |
return f"{speed[:separator_position+3]} kt" | |
return "?" | |
def cleanup_v(v: str) -> str: | |
v = v.replace(",", ".").replace(":", ".") | |
separator_position = v.find(".") | |
if separator_position != -1: | |
return f"{v[:separator_position+2]} v" | |
return "?" | |
def main(): | |
# TODO: Update video URL with your own video | |
url = "" | |
video = pafy.new(url) | |
videoplay = video.getbest(preftype="any") | |
cap = cv2.VideoCapture(videoplay.url) | |
print(f"Video FPS: {cap.get(cv2.CAP_PROP_FPS)}") | |
# In seconds | |
start_time = 8 * 60 | |
end_time = 9 * 60 | |
# Set the start time | |
cap.set(cv2.CAP_PROP_POS_MSEC, start_time * 1000) | |
# Execute specific duration | |
while True and cap.get(cv2.CAP_PROP_POS_MSEC) <= end_time * 1000: | |
success, img = cap.read() | |
if not success: | |
break | |
# Pre-process image | |
img = img[0:360, 0:360] | |
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) | |
gray = cv2.bitwise_not(gray) | |
blur = cv2.GaussianBlur(gray, (5, 5), 0) | |
_, gray = cv2.threshold(blur, 40, 255, cv2.THRESH_BINARY) | |
sharpen_kernel = np.array([[-1, -1, -1], [-1, 9, -1], [-1, -1, -1]]) | |
gray = cv2.filter2D(gray, -1, sharpen_kernel) | |
# Show pre-processed image to screen | |
cv2.imshow("Image", gray) | |
cv2.waitKey(1) | |
# Crop first image | |
cropped_img = gray[0:113, 0:360] | |
# Scale to 25% | |
width = int(cropped_img.shape[1] * 25 / 100) | |
height = int(cropped_img.shape[0] * 25 / 100) | |
resized_img = cv2.resize(cropped_img, (width, height), interpolation = cv2.INTER_AREA) | |
data1 = pytesseract.image_to_string(resized_img, lang='eng', config=TESSERACT_PARAMETERS).lower() | |
data1 = data1.split('\n') | |
# Cleanup data | |
while "" in data1: | |
data1.remove("") | |
if len(data1) == 1 and "m" in data1[0]: | |
data1[0] = cleanup_dist((data1[0])) | |
else: | |
data1 = ["?"] | |
# Crop second image | |
cropped_img = gray[113:360, 0:360] | |
# Extract text with tesseract | |
data2 = pytesseract.image_to_string(cropped_img, lang='eng', config=TESSERACT_PARAMETERS).lower() | |
data2 = data2.split('\n') | |
# Cleanup data | |
while "" in data2: | |
data2.remove("") | |
# Skip broken data | |
if len(data2) != 4: | |
continue | |
if ":" not in data2[3]: | |
continue | |
data2[0] = cleanup_temperature(data2[0]) | |
data2[1] = cleanup_speed(data2[1]) | |
data2[2] = cleanup_v(data2[2]) | |
print(data1 + data2) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Install
youtube-dl
viapip install youtube_dl
Install
pafy
viapip install git+https://github.com/mps-youtube/pafy.git
. This is a workaround for mps-youtube/pafy#306.Install
opencv-python
viapip install opencv-python
In MacOS,
tesseract
can be installed viabrew install tesseract
Install
pytesseract
viapip install pytesseract