Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 14 additions & 15 deletions ci2v.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,13 @@
import cv2

from sys import stdout
from itertools import izip

from skimage import color
from skimage.measure import structural_similarity as ssim
from skimage.metrics import structural_similarity as ssim


#record start time
start = time.clock()
start = time.process_time()

#ignore non-contiguous skimage warning
warnings.filterwarnings("ignore", module="skimage")
Expand Down Expand Up @@ -45,7 +44,7 @@ def parse_video(image, video, n_matches, break_point=False, verbose=False):
frame_count = 0

#get current time
fps_time = time.clock()
fps_time = time.process_time()

cap = cv2.VideoCapture(video)
while(cap.isOpened()):
Expand Down Expand Up @@ -81,7 +80,7 @@ def parse_video(image, video, n_matches, break_point=False, verbose=False):
similarities = similarities[:n_matches]

#calculate fps
fps = frame_count / (time.clock() - fps_time)
fps = frame_count / (time.process_time() - fps_time)

#feedback to cli
stdout.write('\r@ %d [%sfps] | best: %d (%s) \r'
Expand All @@ -99,13 +98,13 @@ def parse_video(image, video, n_matches, break_point=False, verbose=False):

def sort_results(results, output=False):
#sort results
print '\n'
print('\n')
sorted_results = sorted(results, key=operator.itemgetter('similarity'), reverse=True)
n = 0
print '\n--results:'
print('\n--results:')
for res in sorted_results:
n += 1
print '#%s\t%s\t%s\t: %s' % (n, res['filename'], res['frame'], res['similarity'])
print('#%s\t%s\t%s\t: %s' % (n, res['filename'], res['frame'], res['similarity']))

#save matched frames
if output:
Expand All @@ -126,7 +125,7 @@ def walk(source_image, directory, number=1, break_point=False):
for ext in extentions:
if file.endswith(ext):
video_fn = (os.path.join(root, file))
print video_fn
print(video_fn)
similarities = parse_video(source_image,
video_fn,
n_matches=number,
Expand Down Expand Up @@ -181,32 +180,32 @@ def main():
#either walk directory or hande single file
if args.directory:
#scan directory and process each video file
print '\n--reading videos:'
print('\n--reading videos:')
results = walk(source_image, args.directory, args.number, args.break_point)
s_results = sort_results(results, args.output)

else:
#process single video file
print '\n--reading video:'
print('\n--reading video:')
similarities = parse_video(source_image,
args.video,
n_matches=args.number,
break_point=args.break_point)

print '\n\n--results:'
print('\n\n--results:')
#results to cli
n = 0
for d in similarities:
n += 1
print '#%s\t%s\t: %s' % (n, d['frame'], d['similarity'])
print('#%s\t%s\t: %s' % (n, d['frame'], d['similarity']))

#save matched frames
if args.output:
save_frame(args.output, n, d['image'])

seconds_taken = time.clock() - start
seconds_taken = time.process_time() - start
time_taken = str(datetime.timedelta(seconds=seconds_taken))
print '\n--time taken: \n%s\n' % time_taken
print('\n--time taken: \n%s\n' % time_taken)


if __name__ == '__main__':
Expand Down
8 changes: 6 additions & 2 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,15 @@ ci2v searches videos for frames most similar to an image.


## Requirements
* python
* python 3.9 (might work with lower versions, but is untested!)
* numpy
* opencv
* scikit-image

```bash
pip install scikit-image opencv-python numpy
```


## How to use it

Expand Down Expand Up @@ -83,4 +87,4 @@ Search recursively through the specified directory for video files and process e

ci2v is built around **OpenCV** and **scikit-image**, and uses the latter's [*Structural similarity index*](http://scikit-image.org/docs/dev/auto_examples/plot_ssim.html) function to compare an input image to a video frame.

Because `ssim` can be slow with large images, ci2v rescales images to 10x10px greyscale arrays. This means ci2v can search a video at around 200fps and still find acceptable matches.
Because `ssim` can be slow with large images, ci2v rescales images to 10x10px greyscale arrays. This means ci2v can search a video at around 200fps and still find acceptable matches.