In each prosecution for a violation of a state law or a municipal or county ordinance punishable by imprisonment, the defendant shall have, upon demand, the right to a trial by an impartial jury in the county where the offense was committed, except as to any such prosecution for a violation punishable for a term of imprisonment of 6 months or less, if at the time the case is set for trial the court announces that in the event of conviction of the crime as charged or of any lesser included offense a sentence of imprisonment will not be imposed and the defendant will not be adjudicated guilty, unless a right to trial by jury for such offense is guaranteed under the State or Federal Constitution.