Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #42474

Re: Error in working with Dict

Path csiph.com!usenet.pasdenom.info!gegeweb.org!usenet-fr.net!nerim.net!novso.com!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <davea@davea.name>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.002
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'else:': 0.03; 'elif': 0.05; 'tree': 0.05; 'subject:Error': 0.07; 'advance': 0.07; '"__main__":': 0.09; '#print': 0.09; '[0,': 0.09; 'blue': 0.09; 'function,': 0.09; 'pixel': 0.09; 'trees': 0.09; 'def': 0.12; '"w")': 0.16; '49,': 0.16; '65536': 0.16; 'add,': 0.16; 'dictionaries': 0.16; 'dot,': 0.16; 'function?': 0.16; 'itertools': 0.16; 'nameerror:': 0.16; 'numpy': 0.16; 'wrote:': 0.18; 'import': 0.22; 'print': 0.22; 'header:User-Agent:1': 0.23; 'error': 0.23; 'math': 0.24; 'skip:" 30': 0.26; 'pass': 0.26; 'defined': 0.27; 'header:In-Reply-To:1': 0.27; 'function': 0.29; 'am,': 0.29; 'points': 0.29; 'getting': 0.31; "skip:' 10": 0.31; 'branches': 0.31; 'initialized': 0.31; 'file': 0.32; 'class': 0.32; 'skip:c 30': 0.32; '(most': 0.33; 'skip:t 40': 0.33; 'subject:with': 0.35; "can't": 0.35; 'skip:s 30': 0.35; 'but': 0.35; 'really': 0.36; 'done': 0.36; 'thanks': 0.36; 'to:addr :python-list': 0.38; 'recent': 0.39; 'realize': 0.39; 'to:addr:python.org': 0.39; 'how': 0.40; 'even': 0.60; 'till': 0.61; 'mentioned': 0.61; 'traffic': 0.61; 'name': 0.63; 'road': 0.65; 'floor,': 0.68; 'received:74.208': 0.68; 'back?': 0.84; 'dict.': 0.84; 'gray': 0.84; 'poles': 0.84; 'received:74.208.4.194': 0.84; 'trunks': 0.84; 'buildings': 0.91
Date Mon, 01 Apr 2013 13:33:36 -0400
From Dave Angel <davea@davea.name>
User-Agent Mozilla/5.0 (X11; Linux x86_64; rv:17.0) Gecko/20130308 Thunderbird/17.0.4
MIME-Version 1.0
To python-list@python.org
Subject Re: Error in working with Dict
References <CAFqGZRFgAVHTjA+XcoGRg+p2QcGZP_t94tKaem+BWAdj+p0oiQ@mail.gmail.com>
In-Reply-To <CAFqGZRFgAVHTjA+XcoGRg+p2QcGZP_t94tKaem+BWAdj+p0oiQ@mail.gmail.com>
Content-Type text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding 7bit
X-Provags-ID V02:K0:bclgZ/wEzWXF9uET/JG50lwszzZJbqMeq3tn6w7Bnve Nc3tqSF8bI9qhvyyBjYtV2Z/WFtmAJd4iK/2x3+G3gm5Wc691B QtWrjAJ6CHsVBjhlqp3PwyAxe5Ttq95TLxmsWPLI97WBt2RJjc BkGdhA/UczdHYyj7H1RH8cRwgWhBIdosyW/lswmv+MdCGxyXJK 4pmsar9LRb8v1jDUVThbHpSXX63Xjd5tV3i7HqOyPS0YmnAsOc Zt86W9CA8UF3v4mkJ205BUwTwm2zDQicPMoS9TKRbRsWEKPP+r fmmEkn0oWRdvIaPdsEtWw7FpNyj+SKN8Am6K5+AuHFukwEsMg= =
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.4.1364837636.17481.python-list@python.org> (permalink)
Lines 97
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1364837636 news.xs4all.nl 6912 [2001:888:2000:d::a6]:38055
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:42474

Show key headers only | View raw


On 04/01/2013 09:50 AM, inshu chauhan wrote:
> I have this program which is working with 2 dictionaries segments,
> class_counts.. but I am getting an error mentioned below the programme.
>
> import cv
> from itertools import *
> from math import floor, sqrt, ceil
> from numpy import array, dot, subtract, add, outer, argmax, linalg as lin
>
>
> def Computesegclass(segimage, refimage):
>      f = open("Pixel_count_with_region_num_trial1.txt", "w")
>      segments = {}
>      class_count = {}
>      for y in xrange(0, segimage.height):
>          for x in xrange(0, segimage.width):
>
>              if segimage[y,x] == (0.0, 0.0, 0.0):
>                  continue
>              else:
>                  seg_color = segimage[y,x]
>                  blue = int(seg_color[0])
>                  green = int(seg_color[1])
>                  red = int(seg_color[2])
>                  region_num = blue + 256 * green + 65536 * red
>                  #print region_num
>                  segments[region_num] = segments.setdefault(region_num, 0) +
> 1
>                  #print segments
>
>                  class_color = refimage[y,x]
>
>                  if class_color == (0.0,0.0,0.0):
>                      class_number = 0      # No class
>                  elif class_color == (0.0,255.0,0.0):
>                      class_number = 1   # Trees
>                  elif class_color == (255.0, 0.0, 128.0):
>                      class_number = 2   # Buildings
>                  elif class_color == (0.0,0.0,255.0):
>                      class_number = 3    # Automobiles
>                  elif class_color == (255.0, 255.0, 0.0):
>                      class_number = 4    # Road Points
>                  elif class_color == (0.0, 64.0, 128.0):
>                      class_number = 5    # Tree trunks nad branches
>                  elif class_color == (255.0, 0.0 ,0.0):
>                      class_number = 6    # Poles
>                  elif class_color == (255.0, 0.0, 255.0):
>                      class_number = 7    # Traffic Lights
>                  else:
>                      class_number = 0     # Gray Pixel
>
>                  class_count.setdefault(region_num, [0, 0, 0, 0, 0, 0, 0,
> 0])[class_number] += 1
>              # print class_count

You do realize you ended the function when you put the following line at 
the left margin?  Perhaps you really intended to put the for loop inside 
the function?  I can't tell.

You don't call Computesegclass() till later, so how can you use its 
dicts, even if it did correctly pass them back?

> for k in sorted(class_count.iterkeys()):
>      i = argmax(class_count[k])
>      print >> f, i
>
> if __name__== "__main__":
>      segimage = cv.LoadImageM(r"C:\Users\inshu\Desktop\Masters
> Thesis\Segmentation\segmentation_numbers_00000.tif",
> cv.CV_LOAD_IMAGE_UNCHANGED)
>      refimage = cv.LoadImageM(r"C:\Users\inshu\Desktop\Masters
> Thesis\Segmentation\Hand_Classified1.tif", cv.CV_LOAD_IMAGE_UNCHANGED)
>      print segimage
>      Computesegclass(segimage, refimage)
>
>
> ERROR :
>
> Traceback (most recent call last):
>    File "C:\Users\inshu\Desktop\seg.py", line 49, in <module>
>      for k in sorted(class_count.iterkeys()):
> NameError: name 'class_count' is not defined
>
> I know this error is because I have initialized both dicts inside the
> function, But why dicts are not saved out, if I intialize the dicts outside
> the function, the processing is still done inside and end result I am
> getting is an empty dict.
>
>
> Thanks in Advance for suggestions!!!!
>
>
>


-- 
DaveA

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: Error in working with Dict Dave Angel <davea@davea.name> - 2013-04-01 13:33 -0400

csiph-web