Skip to content

Commit

Permalink
normal-flow UI complete
Browse files Browse the repository at this point in the history
  • Loading branch information
chpoit committed May 26, 2021
1 parent 4aa98c6 commit b8fa20d
Show file tree
Hide file tree
Showing 4 changed files with 185 additions and 66 deletions.
34 changes: 20 additions & 14 deletions src/charm_extraction.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,28 +175,33 @@ def extract_charm(frame_loc, slots, skills, skill_text):
return charm


def extract_charms(frame_dir,_=lambda x:x, iter_wrapper=None):
def extract_charms(frame_dir, _=lambda x: x, iter_wrapper=None, charm_callback=lambda x: None):
if not iter_wrapper:
iter_wrapper = tqdm
tess = Tesseract()
charms = []
charm_loc = []

try:
frames = list(
map(lambda frame_loc: frame_loc.path, os.scandir(frame_dir)))

def keep_existing_and_update(x):
i, x = x
if x:

return x

with iter_wrapper(frames, desc=_("parsing-skills-slots")) as parse_pbar:
combined_data = list(filter(
lambda x: x,
map(
lambda frame_loc: extract_basic_info(tess,
frame_loc, cv2.imread(
frame_loc)
),
parse_pbar
)
)
)
count = 0
combined_data = []
for frame_loc in parse_pbar:
charm_tuple = extract_basic_info(
tess, frame_loc, cv2.imread(frame_loc))
if charm_tuple:
count += 1
charm_callback({"charm_count": count})
combined_data.append(charm_tuple)

with iter_wrapper(combined_data, desc=_("validate-fix")) as build_pbar:
for frame_loc, slots, skills, skill_text in build_pbar:
Expand All @@ -208,12 +213,13 @@ def extract_charms(frame_dir,_=lambda x:x, iter_wrapper=None):
else:
logger.warn(_("logger-skill-less").format(frame_loc))
except Exception as e:
logger.error(_("logger-charm-error").format(frame_loc, e) )
logger.error(_("logger-charm-error").format(frame_loc, e))

except Exception as e:
logger.error(f"Crashed with {e}")

unique_charms = CharmList(charms)
charm_callback({"unique_charms": len(unique_charms)})
if len(charms) != len(unique_charms):
print("Pre-duplicate", len(charms))
print("Post-duplicate:", len(unique_charms))
Expand Down Expand Up @@ -260,7 +266,7 @@ def extract_basic_info(tess: Tesseract, frame_loc, frame):
return None


def save_charms(charms:CharmList, charm_json):
def save_charms(charms: CharmList, charm_json):
with open(charm_json, "w") as charm_file:
json.dump(charms.to_dict(), charm_file)

Expand Down
48 changes: 26 additions & 22 deletions src/frame_extraction.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,68 +61,70 @@ def extract_unique_frames(input_dir, frame_dir, _=lambda x: x, iter_wrapper=None
if not iter_wrapper:
iter_wrapper = tqdm

charm_count = 0
currentFrame = 0
frame_count = 0
current_frame = 0
seq_count = 0

input_files = list(
filter(lambda x: is_validated_video_format(x.name), os.scandir(input_dir)))
print(_("total-input").format(len(input_files)))

frame_prog_item = {"total_files": len(input_files), "current_file": 0}
frame_callback({"total_files": len(input_files), "current_file": 0})

all_unique_frames = []

for f_loc in input_files:
for file_no, f_loc in enumerate(input_files):
f_name = f_loc.name
f_loc = f_loc.path
frame_prog_item["f_name"] = f_name
frame_callback(frame_prog_item)

cap = cv2.VideoCapture(f_loc)
frame_count = floor(cap.get(cv2.CAP_PROP_FRAME_COUNT))
if cap.get(cv2.CAP_PROP_FPS) == 60:
print(_("60-fps"))
frame_count /= 2
frame_count = floor(frame_count/2)

frame_callback({"f_name": f_name, "frame_count": frame_count})

previous_charm_marker = None
with iter_wrapper(crop_frames(cap), total=floor(frame_count), desc=_("fn-total-charm").format(f_name, charm_count)) as frame_pbar:
with iter_wrapper(crop_frames(cap), total=frame_count, desc=_("fn-total-charm").format(f_name, frame_count)) as frame_pbar:
for i, cropped_tuple in frame_pbar:
cropped, charm_only = cropped_tuple

if previous_charm_marker is not None:

if is_new_frame(previous_charm_marker, charm_only):
charm_count += 1
seq_count += 1
all_unique_frames.append(
(currentFrame, cropped, charm_only))
(current_frame, cropped, charm_only))
else:
charm_count += 1
seq_count += 1
all_unique_frames.append(
(currentFrame, cropped, charm_only))
(current_frame, cropped, charm_only))

previous_charm_marker = charm_only

frame_pbar.set_description(
_("fn-total-charm").format(f_name, charm_count))
currentFrame += 1
_("fn-total-charm").format(f_name, current_frame))
current_frame += 1

frame_prog_item["charm_count"] = charm_count
frame_prog_item["current_frame"] = currentFrame
frame_callback(frame_prog_item)
frame_callback({
"frame_count":frame_count,
"current_frame": current_frame,
"seq": seq_count})

cap.release()
cv2.destroyAllWindows()
frame_prog_item["current_file"] += 1

frame_callback(frame_prog_item)
frame_callback({"file_no": file_no})

frame_callback({"f_name": _("done-scanning")})

non_seq = 0
with iter_wrapper(range(len(all_unique_frames)), desc=_("detect-non-seq")) as pbar:
for i in pbar:
is_new = True
sourceNo, sourceCrop, sourceCharmOnly = all_unique_frames[i]
for j in range(i+1, len(all_unique_frames)):
_, cropped, charm_only = all_unique_frames[j]
_unused, cropped, charm_only = all_unique_frames[j]
is_new = is_new_frame(sourceCharmOnly, charm_only)
if not is_new:
break
Expand All @@ -131,7 +133,9 @@ def extract_unique_frames(input_dir, frame_dir, _=lambda x: x, iter_wrapper=None
name = os.path.join(frame_dir, f"frame{sourceNo}.png")
cv2.imwrite(name, sourceCrop)

print(_("non-seq-diff").format(charm_count, non_seq))
frame_callback({"non_seq": non_seq})

print(_("non-seq-diff").format(frame_count, non_seq))


if __name__ == "__main__":
Expand Down
Loading

0 comments on commit b8fa20d

Please sign in to comment.