Skip to content

Instantly share code, notes, and snippets.

@dfar-io
Last active October 24, 2024 23:12
Show Gist options
  • Save dfar-io/523b38d163fd17478974b21630737a48 to your computer and use it in GitHub Desktop.
Save dfar-io/523b38d163fd17478974b21630737a48 to your computer and use it in GitHub Desktop.
# before running:
#
# 1. Install dependencies:
# cd remnant-2-video-analyzer
# pip install opencv-python pytesseract
# 2. Install Tesseract (https://github.com/tesseract-ocr/tesseract)
# 3. Run python script:
# python main.py
import cv2
import pytesseract
import sys
# Path to the Tesseract executable (modify if necessary)
pytesseract.pytesseract.tesseract_cmd = r'C:\Program Files\Tesseract-OCR\tesseract.exe' # Adjust for your installation
bosses = [
# bosses
'Abomination',
'Bloat King',
'Bruin, Blade of the King',
'Cancer',
'Cinderclad Forge',
'Gwendil: The Unburnt',
'Kaeula\'s Shadow',
'Legion',
'Magister Dullain',
'Mother Mind',
'N\'Erudian Reaper',
'Primogenitor',
'Shrewd',
'Amalgam Kel\'Roth',
'The Astropath',
'Custodian\'s Eye',
'The Huntress',
'The Red Prince',
'The Stonewarden',
'Sunken Witch',
'Venom',
# world bosses
'Alepsis-Taura',
'Annihilation',
'Corrupted Ravager',
'Corruptor',
'Imposter King',
'Labyrinth Sentinel',
'Lydusa',
'Sha\'Hala: Spectral Guardian of N\'Erud',
'Sha\'Hala: Guardian of N\'Erud',
'Tal\'Ratha',
'Tal\'Ratha (Metaphysical)',
'The Nightweaver',
'The One True King',
# aberrations
'Arcanum Diviner',
'Barghest the Vile',
'Blightspawn',
'Firth: The Oathkeeper',
'Gorge',
'Grimshot',
'Highborn Stalker',
'Maleficent Glimmer',
'Mournshot',
'Ripsaw',
'Scorchslinger',
'Seer of the Veil',
'The Executioner',
'Tortured Flame',
'Vilethorn Brawler',
'Vilethorn Snapper',
'Fetid Corpse',
'Restless Spirit',
'W.D. 109',
'The Progeny',
'E.D. Alpha',
'Defiler',
'Rot',
'Wither',
'Mantagora',
'The Weald Stalker',
'The Gnarled Archer',
'Fester',
'Atrophy',
'Rot Stalker',
'Plaguebringer',
'Talonscythe',
'Bastion',
'Bane'
]
class Result:
def __init__(self, boss_name, start_time):
self.boss_name = boss_name
self.start_time = start_time
def detect_text_in_video(video_path):
results = []
# Open the video file
cap = cv2.VideoCapture(video_path)
total_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
if not cap.isOpened():
print("Error: Could not open video.")
return
frame_number = 0
while True:
# Read a frame from the video
cap.set(cv2.CAP_PROP_POS_FRAMES, frame_number)
ret, frame = cap.read()
if not ret:
break # Exit if no frames are left
x, y, width, height = 1100, 0, 1650, 250
roi = frame[y:y + height, x:x + width]
frame_number += 60
# Convert the frame to gray scale (optional, but can help with OCR)
gray_frame = cv2.cvtColor(roi, cv2.COLOR_BGR2GRAY)
# Use Tesseract to do OCR on the frame
text = pytesseract.image_to_string(gray_frame)
# Check for the boss name in the frame
for boss in bosses:
if boss in text:
result_bosses = [result.boss_name for result in results]
if boss not in result_bosses:
seconds = int(frame_number / 60)
time = seconds_to_hh_mm_ss(seconds)
results.append(Result(boss, time))
progress_bar(frame_number, total_frames)
# Release the video capture object
cap.release()
cv2.destroyAllWindows()
print()
print()
for result in results:
formatted_string = "{:<30} | {:<10}".format(result.boss_name, result.time)
print(formatted_string)
def progress_bar(iteration, total, length = 50):
percent = iteration / total
percent = 1 if percent > 1 else percent
progress = int(length * iteration / total)
bar = '=' * progress + '-' * (length - progress)
percent_progress = 100 * iteration / total
sys.stdout.write(f'\r[{bar}] {percent_progress:.2f}%')
sys.stdout.flush()
def seconds_to_hh_mm_ss(total_seconds):
hours = total_seconds // 3600
minutes = (total_seconds % 3600) // 60
seconds = total_seconds % 60
return f"{hours:02}:{minutes:02}:{seconds:02}"
# Example usage - looking for a file alongside the script
video_file = 'ivha.mp4'
detect_text_in_video(video_file)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment