Python Forum

Full Version: Problem with reading a path
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2 3 4
(Jan-04-2019, 05:00 PM)nilamo Wrote: [ -> ]So the files you're trying to open are in C:\Users\gonca\Anaconda3\lib\site-packages\Desktop\cityscape and C:\Users\gonca\Anaconda3\lib\site-packages\Desktop\landscape, correct? If not, then I'd suggest either using os.chdir() to change where you think you are to match where your file actually is, or using absolute paths to reference your files/folders. Here's an example of using os.chdir():
import os import pathlib current_file = pathlib.Path(__file__) current_location = current_file.parent.absolute() os.chdir(current_location) # references from here on out to relative paths will be relative to wherever this file exists print(os.getcwd())


the files that i'm tring to read are at

C:\Users\gonca\Desktop\landscape
and
C:\Users\gonca\Desktop\cityscape

I've tried to put that address but i've got another error:

error                                     Traceback (most recent call last)
<ipython-input-18-1ea0691ca06e> in <module>
      1 dir1 = os.listdir(r'C:/Users/gonca/Desktop/cityscape')
      2 dir2 = os.listdir(r'C:/Users/gonca/Desktop/landscape')
----> 3 name,img = uploadImg(dir1, dir2)
 
<ipython-input-17-831c56f87f59> in uploadImg(dir1, dir2)
     10 
     11             orig = cv2.imread(r'C:/Users/gonca/Desktop/landscape' + file)
---> 12             orig = cv2.cvtColor(orig, cv2.COLOR_BGR2RGB)
     13             cityscape = cv2.resize(orig,(512,512))
     14             #cityscape_original.append(cityscape)
 
error: OpenCV(3.4.2) c:\miniconda3\conda-bld\opencv-suite_1534379934306\work\modules\imgproc\src\color.hpp:253: error: (-215:Assertion failed) VScn::contains(scn) && VDcn::contains(dcn) && VDepth::contains(depth) in function 'cv::CvtHelper<struct cv::Set<3,4,-1>,struct cv::Set<3,4,-1>,struct cv::Set<0,2,5>,2>::CvtHelper'
i've put the address like this:



from sklearn.decomposition import NMF
from matplotlib import pyplot as plt
import cv2
import numpy as np
import os
import sys
import statistics
 
 
## upload imgs e returns names e imgs
def uploadImg(dir1,dir2):
    cityscape_original = []
    landscape_original = []
    nameList = [[] for _ in range(512)]
    imgList = [[] for _ in range(512)]
    for file in os.listdir(r'C:/Users/gonca/Desktop/landscape'):
        if file.endswith(".jpg"):
            nameList[0].append(file)
             
            orig = cv2.imread(r'C:/Users/gonca/Desktop/landscape' + file)
            orig = cv2.cvtColor(orig, cv2.COLOR_BGR2RGB)
            cityscape = cv2.resize(orig,(512,512))
            #cityscape_original.append(cityscape)
            imgList[0].append(cityscape)
    for file in os.listdir(r'C:/Users/gonca/Desktop/cityscape'):
        if file.endswith(".jpg"):
            nameList[1].append(file)
            orig = cv2.imread(r'C:/Users/gonca/Desktop/cityscape' + file)
            orig = cv2.cvtColor(orig, cv2.COLOR_BGR2RGB)
 
            landscape = cv2.resize(orig,(512,512))
            #landscape_original.append(landscape)
            imgList[1].append(landscape)
    return nameList,imgList
 
 
dir1 = os.listdir(r'C:/Users/gonca/Desktop/cityscape')
dir2 = os.listdir(r'C:/Users/gonca/Desktop/landscape')
name,img = uploadImg(dir1, dir2)
add /

C:/Users/gonca/Desktop/landscape/' + file

C:/Users/gonca/Desktop/cityscape/' + file
(Jan-04-2019, 06:21 PM)Axel_Erfurt Wrote: [ -> ]add / C:/Users/gonca/Desktop/landscape/' + file C:/Users/gonca/Desktop/cityscape/' + file

dir1 = os.listdir('C:/Users/gonca/Desktop/cityscape/' + file)
dir2 = os.listdir('C:/Users/gonca/Desktop/landscape/' + file)
name,img = uploadImg(dir1, dir2)
it says:

NameError                                 Traceback (most recent call last)
<ipython-input-6-6272acfbcf93> in <module>
----> 1 dir1 = os.listdir('C:/Users/gonca/Desktop/cityscape/' + file)
      2 dir2 = os.listdir('C:/Users/gonca/Desktop/landscape/' + file)
      3 name,img = uploadImg(dir1, dir2)

NameError: name 'os' is not defined
add

import os

on top
(Jan-04-2019, 06:39 PM)Axel_Erfurt Wrote: [ -> ]add import os on top

I've already put that import since i started
Ok but the error is saying the module was never imported. So if it was imported, please share all of your code as it currently exists.
(Jan-04-2019, 06:52 PM)nilamo Wrote: [ -> ]Ok but the error is saying the module was never imported. So if it was imported, please share all of your code as it currently exists.


this is the complete code

from sklearn.decomposition import NMF
from matplotlib import pyplot as plt
import cv2
import numpy as np
import os
import sys
import statistics


def uploadImg(dir1,dir2):
    cityscape_original = []
    landscape_original = []
    nameList = [[] for _ in range(512)]
    imgList = [[] for _ in range(512)]
    for file in os.listdir('C:/Users/gonca/Desktop/landscape/'):
        if file.endswith(".jpg"):
            nameList[0].append(file)
            
            orig = cv2.imread('C:/Users/gonca/Desktop/landscape/' + file)
            orig = cv2.cvtColor(orig, cv2.COLOR_BGR2RGB)
            cityscape = cv2.resize(orig,(512,512))
            #cityscape_original.append(cityscape)
            imgList[0].append(cityscape)
    for file in os.listdir('Desktop/cityscape'):
        if file.endswith(".jpg"):
            nameList[1].append(file)
            orig = cv2.imread('C:/Users/gonca/Desktop/cityscape/')
            orig = cv2.cvtColor(orig, cv2.COLOR_BGR2RGB)

            landscape = cv2.resize(orig,(512,512))
            #landscape_original.append(landscape)
            imgList[1].append(landscape)
    return nameList,imgList

dir1 = os.listdir('C:/Users/gonca/Desktop/cityscape/' + file)
dir2 = os.listdir('C:/Users/gonca/Desktop/landscape/' + file)
name,img = uploadImg(dir1, dir2)
and the error is:

NameError                                 Traceback (most recent call last)
<ipython-input-9-5b073e198060> in <module>
      1 
----> 2 dir1 = os.listdir('C:/Users/gonca/Desktop/cityscape/' + file)
      3 dir2 = os.listdir('C:/Users/gonca/Desktop/landscape/' + file)
      4 name,img = uploadImg(dir1, dir2)

NameError: name 'file' is not defined
file is only defined in the function. It does not exist outside the function. You need to return it from the function to use it elsewhere.

Also, file is a really bad variable name. file is a built-in type in Python, and be assigning something else to it you lose the use of the built-in, as might other code that is expecting it.
(Jan-05-2019, 01:49 AM)ichabod801 Wrote: [ -> ]file is only defined in the function. It does not exist outside the function. You need to return it from the function to use it elsewhere. Also, file is a really bad variable name. file is a built-in type in Python, and be assigning something else to it you lose the use of the built-in, as might other code that is expecting it.

Initialy i didin't have '+ file' it was an advice from Axel because i can't read the path
Are you using Anaconda3 or miniconda?
Error:
error: OpenCV(3.4.2) c:\miniconda3\conda-bld\opencv-suite_1534379934306\...
The error comes from miniconda,but you have also have C:\Users\gonca\Anaconda3.
You know that Anaconda3(full version 1400+ packages) and miniconda3(0 packages have to install with conda or pip) are different standalone versions?
ichabod801 Wrote:Also, file is a really bad variable name. file is a built-in type in Python
In Python 3 it's okay to use file,in python 2 it was not.
# Python 2.7
>>> file
<type 'file'>  

# Python 3.7
>>> file
Traceback (most recent call last):
  File "<interactive input>", line 1, in <module>
NameError: name 'file' is not defined
I have used file many times in Python 3,even if old habits sometime hit in and tells that's is wrong when it really is not Undecided
Pages: 1 2 3 4