RGB to Grayscale attribute error

Discussion related to "under the hood" OpenMV topics.
FeKTapano
Posts: 10
Joined: Tue Jan 02, 2018 8:39 pm

RGB to Grayscale attribute error

Postby FeKTapano » Fri Nov 08, 2019 7:03 pm

Good evening.
I am working on a project that requires rgb to grayscale tuple conversion, however I am getting ("AttributeError: 'Image' object has no attribute 'rgb_to_grayscale') after uncommenting line 58 of the following code:

Code: Select all

import sensor, image, time, math
from pyb import LED,UART,Pin
sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QQSIF)
sensor.set_auto_gain(True)
sensor.set_auto_whitebal(True)
sensor.set_auto_exposure(True)
sensor.set_contrast(3)
sensor.set_saturation(3)
ImageX = 88
ImageY = 60
letter_thresholds = (0, 50)
color_thresholds = [(30, 100, 15, 127, 15, 127), (30, 100, -64, -8, -32, 32)]
uart = UART(3, 9600, timeout_char=10)
uart.init(9600)
clock = time.clock()
blobHtop = 255
blobHbottom = 255
blobHleft = 0
blobHright = 0
blobHtopleft = 0
blobHtopright = 0
blobHbottomleft = 0
blobHbottomright = 0
blobStop = 0
blobSbottom = 0
blobSleft = 0
blobSright = 0
blobStopleft = 255
blobStopright = 255
blobSbottomleft = 255
blobSbottomright = 255
blobUtop = 255
blobUbottom = 0
blobUleft = 0
blobUright = 0
blobUtopleft = 0
blobUtopright = 0
blobUbottomleft = 255
blobUbottomright = 255

while(True):
    clock.tick()
    img = sensor.snapshot()
    color_blobfound=False
    for blob in img.find_blobs(color_thresholds, area_threshold=150, merge=False):
        x=1
        #sdasdaadsf
    img.cartoon(size=1, seed_threshold=1)
    img.binary([letter_thresholds], zero=True)
    letter_blobfound=False
    for blob in img.find_blobs([letter_thresholds], area_threshold=150, merge=False):
        if(img.get_statistics(roi=blob.rect()).stdev()<30):
            continue
        if(max(blob.w(), blob.h())/min(blob.w(), blob.h())<1.6 and blob.cx()+(blob.w()/2)<ImageX and blob.cx()-(blob.w()/2)>0 and blob.cy()-(blob.h()/2)>0 and blob.cy()-(blob.h()/2)<ImageY and math.sqrt(((blob.cx()-(ImageX/2))*(blob.cx()-(ImageX/2)))+((blob.cy()-(ImageY/2))*(blob.cy()-(ImageY/2))))<(ImageY/3)):
            letter_blobfound=True
            #SimilarityH = 255-((abs(img.rgb_to_grayscale(img.get_pixel(blob.cx(), blob.y()))-blobHtop)+abs(img.rgb_to_grayscale(img.get_pixel(blob.cx(), blob.y()+blob.h()))-blobHbottom)+abs(img.rgb_to_grayscale(img.get_pixel(blob.x(), blob.cy()))-blobHleft)+abs(img.rgb_to_grayscale(img.get_pixel(blob.x()+blob.w(), blob.cy()))-blobHright)+abs(img.rgb_to_grayscale(img.get_pixel(blob.x(), blob.y()))-blobHtopleft)+abs(img.rgb_to_grayscale(img.get_pixel(blob.x()+blob.w(), blob.y()))-blobHtopright)+abs(img.rgb_to_grayscale(img.get_pixel(blob.x(), blob.y()+blob.h()))-blobHbottomleft)+abs(img.rgb_to_grayscale(img.get_pixel(blob.x()+blob.w(), blob.y()+blob.h()))-blobHbottomright))/8)
            #SimilarityS = 255-((abs(img.rgb_to_grayscale(img.get_pixel(blob.cx(), blob.y()))-blobStop)+abs(img.rgb_to_grayscale(img.get_pixel(blob.cx(), blob.y()+blob.h()))-blobSbottom)+abs(img.rgb_to_grayscale(img.get_pixel(blob.x(), blob.cy()))-blobSleft)+abs(img.rgb_to_grayscale(img.get_pixel(blob.x()+blob.w(), blob.cy()))-blobSright)+abs(img.rgb_to_grayscale(img.get_pixel(blob.x(), blob.y()))-blobStopleft)+abs(img.rgb_to_grayscale(img.get_pixel(blob.x()+blob.w(), blob.y()))-blobStopright)+abs(img.rgb_to_grayscale(img.get_pixel(blob.x(), blob.y()+blob.h()))-blobSbottomleft)+abs(img.rgb_to_grayscale(img.get_pixel(blob.x()+blob.w(), blob.y()+blob.h()))-blobSbottomright))/8)
            #SimilarityU = 255-((abs(img.rgb_to_grayscale(img.get_pixel(blob.cx(), blob.y()))-blobUtop)+abs(img.rgb_to_grayscale(img.get_pixel(blob.cx(), blob.y()+blob.h()))-blobUbottom)+abs(img.rgb_to_grayscale(img.get_pixel(blob.x(), blob.cy()))-blobUleft)+abs(img.rgb_to_grayscale(img.get_pixel(blob.x()+blob.w(), blob.cy()))-blobUright)+abs(img.rgb_to_grayscale(img.get_pixel(blob.x(), blob.y()))-blobUtopleft)+abs(img.rgb_to_grayscale(img.get_pixel(blob.x()+blob.w(), blob.y()))-blobUtopright)+abs(img.rgb_to_grayscale(img.get_pixel(blob.x(), blob.y()+blob.h()))-blobUbottomleft)+abs(img.rgb_to_grayscale(img.get_pixel(blob.x()+blob.w(), blob.y()+blob.h()))-blobUbottomright))/8)
    if(color_blobfound==True):
        x=1
        #asdasodans
    elif(letter_blobfound==True):
        if(SimilarityS>=SimilarityH and SimilarityS>=SimilarityU):
            print('S')
            uart.write('S')
        elif(SimilarityU>=SimilarityH and SimilarityU>=SimilarityS):
            print('U')
            uart.write('U')
        else:
            print('H')
            uart.write('H')
    else:
        print(clock.fps())
        uart.write('n')
I have already updated the camera firmware. Do you have any idea of why this could be happening?
User avatar
kwagyeman
Posts: 3448
Joined: Sun May 24, 2015 2:10 pm

Re: RGB to Grayscale attribute error

Postby kwagyeman » Fri Nov 08, 2019 9:28 pm

It's the image module that has that method. Not the img object.
Nyamekye,

Return to “Technical Discussion”

Who is online

Users browsing this forum: No registered users and 2 guests