I have a stream of images and have to display it in Google Colab notebook such that it looks like a video, But what I get is a image under image …
JavaScript
x
23
23
1
from google.colab import drive
2
drive.mount('/content/drive')
3
4
# importing cv2
5
import cv2
6
import imutils
7
from google.colab.patches import cv2_imshow
8
from IPython.display import clear_output
9
import os
10
11
folder = r'/content/drive/images/'
12
13
for filename in os.listdir(folder) :
14
VALID_FORMAT = (".jpg", ".JPG", ".jpeg", ".JPEG", ".png", ".PNG")
15
if filename.upper().endswith(VALID_FORMAT):
16
path = folder + filename
17
image = cv2.imread(path)
18
# resize image
19
frame = imutils.resize(image, width=1200)
20
# show the image
21
cv2_imshow(frame)
22
cv2.waitKey(20)
23
Advertisement
Answer
I don’t know if some function can display image in the same place.
But I have code which I used with cv2 to display frames from webcam as video.
Here reduced version.
imshow(name, image)
creates <img id="name">
and replaces src/url
with image converted to string base64
and browser shows it as image.
imshow()
uses name
to check if already exist <img id="name">
and it replaces previous image.
JavaScript
1
33
33
1
from IPython.display import display, Javascript
2
from google.colab.output import eval_js
3
from base64 import b64encode
4
import cv2
5
6
def imshow(name, img):
7
"""Put frame as <img src="data:image/jpg;base64, ."> """
8
9
js = Javascript('''
10
async function showImage(name, image, width, height) {
11
img = document.getElementById(name);
12
if(img == null) {
13
img = document.createElement('img');
14
img.id = name;
15
document.body.appendChild(img);
16
}
17
img.src = image;
18
img.width = width;
19
img.height = height;
20
}
21
''')
22
23
height, width = img.shape[:2]
24
25
ret, data = cv2.imencode('.jpg', img) # compress array of pixels to JPG data
26
data = b64encode(data) # encode base64
27
data = data.decode() # convert bytes to string
28
data = 'data:image/jpg;base64,' + data # join header ("data:image/jpg;base64,") and base64 data (JPG)
29
30
display(js)
31
eval_js(f'showImage("{name}", "{data}", {width}, {height})') # run JavaScript code to put image (JPG as string base64) in <img>
32
# `name` and `data` in needs `" "` to send it as text, not as name of variabe.
33
And here code which uses it to display image Lenna from Wikipedia.
JavaScript
1
18
18
1
import requests
2
import cv2
3
import numpy as np
4
import time
5
6
url = 'https://upload.wikimedia.org/wikipedia/en/7/7d/Lenna_%28test_image%29.png'
7
8
data = requests.get(url)
9
10
frame1 = cv2.imdecode(np.frombuffer( data.content, np.uint8), 1)
11
frame2 = cv2.cvtColor(frame1, cv2.COLOR_BGR2GRAY)
12
13
for _ in range(3):
14
imshow("temp", frame1)
15
time.sleep(1)
16
imshow("temp", frame2)
17
time.sleep(1)
18
EDIT
Display images in two “windows” using imshow("img1", ...)
and imshow("img2", ...)
JavaScript
1
22
22
1
import os
2
import cv2
3
import imutils
4
import time
5
6
folder = r'/content/drive/images/'
7
8
VALID_FORMAT = (".JPG", ".JPEG", ".PNG")
9
10
for number, filename in enumerate(os.listdir(folder)):
11
if filename.upper().endswith(VALID_FORMAT):
12
path = os.path.join(folder, filename)
13
14
image = cv2.imread(path)
15
16
frame = imutils.resize(image, width=400)
17
18
number = number % 2
19
imshow(f"img{number}", frame)
20
21
time.sleep(1)
22