In a user experiment, I wish to ask the user to rate images on a scale of 1 to 5. The user will be given instructions at the beginning of the experiment. On the screen, an image will be displayed and the user is supposed to input a value from 1 to 5 through keyboard without any dialog box or prompt.

How can I do this?

  • 1
    $\begingroup$ You can set NotebookEventActions just before and unset it after a number is chosen. $\endgroup$ – Kuba Mar 21 '17 at 19:51

One way to do this is to use an event handler to manage the keystrokes. The images, the current rating and the image's index (in the list of images presented to the user, are all inserted in a graphics pane with Inset.

Test images

A set of test images to be rated is needed for this application. I choose to use a set of RGB test images which are 512 x 512 pixels that can be retrieved with ExampleData. It turns out there are 8 such images, which is a reasonable number.

descriptors = ExampleData["TestImage"];
allImages = ExampleData /@ descriptors;
colorImages =
  Pick[allImages, ImageMeasurements[#, "ColorSpace"] & /@ allImages, "RGB"];
colorImages512 =
    ImageMeasurements[#, "Dimensions"] & /@ colorImages, {512, 512, 3}]
Multicolumn[colorImages512, {4, 2}, Appearance -> "Horizontal"]


Image rating and sequencing by keystrokes

  • Current rating appears at bottom left.
  • Image index appears at bottom right.
  • The graphics pane must have the focus for the event handler to get the keystrokes; i.e., the graphic pane must be selected with the mouse. It will show that it has the focus by displaying a thin red border.
  • Ratings must be integers in the range 1 – 5.
  • Hitting Return brings up the next image in the set. After the last image, the first image is the next image.

ratings = ConstantArray["-", Length[colorImages512]];
With[{n = Length[colorImages512]},
  DynamicModule[{i = 1, keystroke},
      Dynamic @
          {Inset[colorImages512[[i]], {0, 0}, {0, 0}, {512, 512}],
             Style[ ratings[[i]], 20, Background -> White], 
             Scaled[{.05, .05}], {0, 0}, {25, 30}, 
             Background -> White],
             Style[ i, 20, Background -> White], 
             Scaled[{.95, .05}], {0, 0}, {25, 30}, 
             Background -> White]},
          PlotRange -> {{-1, 513}, {-1, 513}}],
      {"KeyDown" :>
         (keystroke = CurrentValue["EventKey"];
            "\r", i = If[Mod[i, n] === 0, 1, i + 1],
            Alternatives @@ (ToString /@ Range[n]), ratings[[i]] = keystroke])}]]]



{"3", "1", "4", "5", "5", "2", "3", "4"}

|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.