Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

import cv2 

import numpy as np 

from imutils.convenience import url_to_image 

from hutts_utils.hutts_logger import logger 

 

 

def grab_image(path=None, stream=None, url=None): 

""" 

This function grabs the image from URL, or image path and applies necessary changes to the grabbed 

images so that the image is compatible with OpenCV operation. 

Author(s): 

Stephan Nell 

Args: 

path (str): The path to the image if it reside on disk 

stream (str): A stream of text representing where on the internet the image resides 

url(str): Url representing a path to where an image should be fetched. 

Raises: 

ValueError: If no path stream or URL was found 

Returns: 

(:obj:'OpenCV image'): Image that is now compatible with OpenCV operations 

""" 

# If the path is not None, then load the image from disk. Example: payload = {"image": open("id.jpg", "rb")} 

if path is not None: 

logger.debug("Grabbing from Disk") 

image = cv2.imread(path) 

 

if image is None: 

raise ValueError('Invalid Path. No image could be found.' 

' Either path is incorrect or image does not exist') 

# otherwise, the image does not reside on disk 

else: 

# If the URL is not None, then download the image 

33 ↛ 36line 33 didn't jump to line 36, because the condition on line 33 was never true if url is not None: 

# Example: "http://www.pyimagesearch.com/wp-content/uploads/2015/05/obama.jpg" 

# If URL is incorrect Urllib in imutils library will catch the error. 

logger.debug("Downloading image from URL") 

return url_to_image(url) 

# if the stream is not None, then the image has been uploaded 

elif stream is not None: 

logger.debug("Downloading from image stream") 

data = stream.read() 

image = np.asarray(bytearray(data), dtype="uint8") 

image = cv2.imdecode(image, cv2.IMREAD_COLOR) 

if image is None: 

raise ValueError('Invalid Path. No image could be found.' 

' Either path is incorrect or image does not exist') 

else: 

raise ValueError('No valid method was found to grab image.' 

' Either path is incorrect or image does not exist') 

 

return image