Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Problem with reading a path
#11
(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)
Reply
#12
add /

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

C:/Users/gonca/Desktop/cityscape/' + file
Reply
#13
(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
Reply
#14
add

import os

on top
Reply
#15
(Jan-04-2019, 06:39 PM)Axel_Erfurt Wrote: add import os on top

I've already put that import since i started
Reply
#16
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.
Reply
#17
(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
Reply
#18
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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#19
(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
Reply
#20
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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Excel file reading problem max70990 1 890 Dec-11-2022, 07:00 PM
Last Post: deanhystad
  Loop reading csv file problem faustineaiden 1 1,564 Dec-11-2021, 08:40 AM
Last Post: ibreeden
  WebDriverException: Message: 'PATH TO CHROME DRIVER' executable needs to be in PATH Led_Zeppelin 1 2,198 Sep-09-2021, 01:25 PM
Last Post: Yoriz
Exclamation Path sacn problem lucky511 10 3,874 Jun-24-2021, 12:09 PM
Last Post: Axel_Erfurt
  Subprocess.Popen() not working when reading file path from csv file herwin 13 14,973 May-07-2021, 03:26 PM
Last Post: herwin
  Strange Problem when reading from excel faryad13 3 2,382 Nov-04-2020, 03:48 PM
Last Post: DeaD_EyE
  problem about maze path finder Simurg 2 1,940 Aug-16-2020, 01:10 PM
Last Post: Simurg
  Reading integers from a file; the problem may be the newline characters JRWoodwardMSW 2 1,963 Jul-14-2020, 02:27 AM
Last Post: bowlofred
  Problem reading data from file player1681 2 3,736 May-22-2020, 08:57 AM
Last Post: player1681
  Problem with reading and writing to file. darktitan 2 2,284 Jul-20-2019, 06:06 PM
Last Post: darktitan

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020