Python hangs when I call imshow - python

from pylab import *
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.cbook as cbook
import random
import time
from scipy.misc import imread
from scipy.misc import imresize
import matplotlib.image as mpimg
import os
# General setup
matplotlib
#gray()
# 2D Array
a = array([[4,5,6], [1,2,3]])
# shape of matrix in format n x m
a.shape
# Read in an image
i = imread('test.jpg')
if __name__ == "__main__":
imshow(i) # HANGS ON OSX
plt.show()
This code runs completely fine on my windows machine, but on MacOSX it hangs whenever I call imshow (even on a trivial array), the image window pops up but it hangs. I am using pycharm.
Anybody know why this is? Am i doing something wrong?

Related

No such file or directory bbox,label,conf = cv.detect_common_objects

from PIL import Image
import numpy as np
import matplotlib.pyplot as plt
import cvlib as cv
from cvlib.object_detection import draw_bbox
import tensorflow
image_path="D:/paper_data/user_picture1/10503.jpeg"
img=Image.open(image_path)
img=np.array(img)
plt.imshow(img)
plt.show()
bbox,label,conf = cv.detect_common_objects(img)
When i run the last code 'bbox,label,conf = cv.detect_common_objects(img)', python shows me the error:FileNotFoundError: [Errno 2] No such file or directory: 'C:\Users\vicky\.cvlib\object_detection\yolo\yolov3\yolov3_classes.txt'
Since this is my first attempt to analyze pictures and my purpose is to retrieve the objects in the picture.
My question is 1. whether there is any package can be used to achieve my purpose 2. what does the error showed above mean?

Is there a way to import and run a function from another script in python that will import libraries for the current script

For example I am trying to import this function "load_modules() which is in a script called "setup.py" into another script using the code directly below.
def load_modules():
import cv2
import numpy as np
import pandas as pd
from scipy import ndimage
import os
from matplotlib import pyplot as plt
import matplotlib.image as mpimg
import time
import math
import random
from scipy.ndimage.interpolation import map_coordinates
from scipy.ndimage.filters import gaussian_filter
from setup import *
load_modules()
img = cv2.imread("image.jpg", 1)
The first two lines run fine so the files functions have been imported and there appears to be no issue running the load_modules() function however I get an error on the last line saying that cv2 is not defined.

Why can't I create a 3d scatter plot in Python on my desktop?

I am trying to use the 3D scatter plot object in python. And I have successfully done this on my laptop. However, I can not copy and paste code onto my desktop. When I do this I get an error. I will attach my the section of my code below that is giving me trouble. I am using Anaconda to run my code. I will note that my laptop uses python 3.6 and my desktop uses 3.7, but I do not think that is causing it. The error I is get is as follows. "ValueError: Unknown projection '3d'"
import numpy as np
from scipy import optimize
import time
from sklearn.metrics import mean_squared_error
import matplotlib.pyplot as plt
import pandas as pd
from sklearn import preprocessing
from sklearn.svm import SVR
import multiprocessing as mp
from obj_class import objective_class
import pdb
import scipy.integrate as integrate
def create3d():
grid_matrix = np.array([[1,1,1,1],[2,2,2,2],[3,3,3,3]])
fig = plt.figure()
ax = plt.axes(projection='3d')
p = ax.scatter3D(grid_matrix[:,0],grid_matrix[:,1] ,grid_matrix[:,2] , c=grid_matrix[:,3], cmap='viridis')
cb = fig.colorbar(p)
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')
ax.set_title(' Scatter Plot')
In order to use a 3d projection in matplotlib <= 3.1 you need to import axes3d, i.e.
from mpl_toolkits.mplot3d import Axes3D
From matplotlib >= 3.2, no extra import is necessary. So possibly you are running different matplotlib versions on both computers.
If you are running your code within an iPython kernel, Jupyter notebook for example,
then you only need to perform each import once and you will be able to run any code which relies on said import until the kernel is shutdown. However, in order to run the script in a self contained fashion you will need that import included in your script.

matplotlib does not show the image

from PIL import Image
import numpy as np
import matplotlib.pyplot as plt
i = Image.open('images/dot.png')
iar = np.asarray(i)
plt.imshow(iar)
plt.show()
I have the image called dot.png in the same directory but it is not working.
When I start the program no errors detected. It runs fine. But the matplotlib tool not working.
The window look like this:

Librosa stops matplotlib from working

I have this simple python 3 example:
import librosa
import numpy as np
import matplotlib.pyplot as plt
# template, Fs = librosa.load('example.wav')
t = np.arange(0, 10)
plt.plot(t)
plt.show()
But as soon as I outcomment librosa.load(...) the program crashes with this error message:
/usr/lib/python3.6/site-packages/matplotlib/backends/backend_qt5.py:124: Warning: g_main_context_push_thread_default: assertion 'acquired_context' failed
qApp = QtWidgets.QApplication([b"matplotlib"])
There is still a new window opened for the plot, but it's completely empty.
Have the exact same problem. Use scipy.io.wavfile.read(path) instead.

Categories

Resources