Skip to content

Instantly share code, notes, and snippets.

@corndog2000
Created June 30, 2019 22:14
Show Gist options
  • Save corndog2000/be8c62e0922dadb6ebcb2e38d92fb87e to your computer and use it in GitHub Desktop.
Save corndog2000/be8c62e0922dadb6ebcb2e38d92fb87e to your computer and use it in GitHub Desktop.
tess-try: Try to classify pictures based on optical-character-recognition if the picture contains certain letters.
# coding=UTF-8
#import the Image library from Pillow
from PIL import Image
#import the tesseract library
import pytesseract
#this is needed to create directories
import os
#library for iterating every file in a directory
import glob
#library to allow me to copy the picture to the new folder
from shutil import copy2
#library to create the progress bar
#from progress.bar import Bar
from progress.bar import IncrementalBar
#ffmpeg library to allow me to make video clips
from moviepy.video.io.ffmpeg_tools import ffmpeg_extract_subclip
from moviepy.editor import *
arguments = str(sys.argv[1:])
arguments = arguments.strip("['")
arguments = arguments.strip("']")
#Folder name to search through
activeFolder = arguments
#Directory that contians the raw video files
videoFile = "/home/jschroedl/Videos/RDR2Videos/" + activeFolder + ".mkv"
#Switch into the correct root directory
os.chdir("/home/jschroedl/Pictures/RDR2/" + activeFolder)
#Get the current working directory
path = os.getcwd()
print("The current working directory is %s" % path)
#If the new Picture directory is already created then keep going. If the folder does not exist then create the folder.
if os.path.exists("PythonPictures"):
print("Directory already exists")
else:
os.mkdir("PythonPictures")
print("Created directory for posible death frames.")
#If the new Video clips directory is already created then keep going. If the folder does not exist then create the folder.
if os.path.exists("PythonVideoClips"):
print("Directory already exists")
else:
os.mkdir("PythonVideoClips")
print("Created directory for video clips.")
onlyfiles = next(os.walk(path))[2]
#Create the loading bar object
bar = IncrementalBar('Processing', max= len(onlyfiles))
#myclip = VideoFileClip(videoFile)
#Loop to go through each file in the folder
for filepath in glob.iglob('*.png'):
print(" Filename: " + filepath)
im = Image.open(filepath)
black = 0
red = 0
for pixel in im.getdata():
if pixel == (0, 0, 0): # if your image is RGB (if RGBA, (0, 0, 0, 255) or so
black += 1
else:
red += 1
#print('black=' + str(black)+', red='+str(red))
text = pytesseract.image_to_string(im, lang = 'eng')
#print("Recognized text: " + text)
if black > 500000 and red > 75000:
if "F" in text or "A" in text or "I" in text or "L" in text or "E" in text or "D" in text:
print("✓✓✓✓✓✓✓✓✓✓✓✓✓✓✓✓✓✓✓✓✓✓✓✓✓✓✓✓✓✓✓✓✓✓✓✓✓✓✓✓✓✓✓✓✓✓✓✓")
print("Found correct letters")
copy2(filepath, "/home/jschroedl/Pictures/RDR2/" + activeFolder + "/PythonPictures/")
#Video clipping
#To make a 60 second clip I start the clip 30 seconds before the frame and end the clip 30 seconds after the frame.
#Remove the rest of the file name and convert the string
stripedFilepath = filepath.strip("output_")
stripedFilepath = int(stripedFilepath.strip(".png"))
print("stripedFilepath=" + str(stripedFilepath))
startTime = (stripedFilepath * 2) - 60
if startTime < 0:
startTime = 0
print("startTime was negative so setting startTime to 0")
endTime = (stripedFilepath * 2) + 30
print("startTime=" + str(startTime))
print("endTime=" + str(endTime))
tf = "/home/jschroedl/Pictures/RDR2/" + activeFolder + "/PythonVideoClips/" + str(stripedFilepath) + ".mkv"
print("tf=" + str(tf))
print("✀✀✀✀✀ CLIPPING THE VIDEO ✀✀✀✀✀")
ffmpeg_extract_subclip(videoFile, int(startTime), int(endTime), targetname = tf)
#videoClip = myclip.subclip(startTime, endTime)
#videoClip.write_videofile(tf, codec="png", threads="3")
print("✓✓✓✓✓ DONE CLIPPING ✓✓✓✓✓")
else:
print("NOPE")
#Progress that loading bar
bar.next()
#After the loop ends finish the loading bar
bar.finish()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment